I have to display the top 50 best sellers and order them from the highest profit to the lowest. I think my query is close but it's still not quite right.
I have 2 tables: products & orderItems.
"products" contains two fields that I need: catalogId (primary key) & productName.
"orderItems" contains a listing of every item we've ever sold. It also has two fields I need: catalogId (foreign key) & unitPrice.
Ok, so here's my query:
-----------------------------------------------------------
select top 50 sum(orderItems.unitPrice) as profit, products.productName as name from orderItems
inner join products on products.catalogid = orderItems.catalogid
group by products.catalogid
order by profit
-------------------------------------------------------------
The "SUM" function is working correctly. I've already verified it. It seems to correctly group the productName with it's corresponding profit however, for some reason, the "order by" isn't working and I'm display 602 records instead of only 50.
Any recomendations?
Thanks.
-Goalie35
I have 2 tables: products & orderItems.
"products" contains two fields that I need: catalogId (primary key) & productName.
"orderItems" contains a listing of every item we've ever sold. It also has two fields I need: catalogId (foreign key) & unitPrice.
Ok, so here's my query:
-----------------------------------------------------------
select top 50 sum(orderItems.unitPrice) as profit, products.productName as name from orderItems
inner join products on products.catalogid = orderItems.catalogid
group by products.catalogid
order by profit
-------------------------------------------------------------
The "SUM" function is working correctly. I've already verified it. It seems to correctly group the productName with it's corresponding profit however, for some reason, the "order by" isn't working and I'm display 602 records instead of only 50.
Any recomendations?
Thanks.
-Goalie35