Forum Moderators: open

Message Too Old, No Replies

Counting "popular items"

need help creating the SQL statement

         

mattglet

4:38 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i have a table that keeps track of all the items sold:

table: OrderItem
pertinent columns: idItem, sTitle, iQuantity, idOrder

i'm trying to make a SQL statement that will loop through the table, and obtain the most popular items sold. the problem i have is that there are duplicate sTitles (due to the fact that different orders can have the same item).

i.e.

idItem - sTitle - iQuantity - idOrder
1 - Blue Widget - 5 - 100
3 - Red Widget - 1 - 23
1 - Blue Widget - 3 - 140
2 - Green Widget - 10 - 132

i need to show that Green Widget is the most popular item (with 10 sold), Blue Widget ranks 2nd (with 8 sold), and Red ranks 3rd (with 1 sold). can anyone help? i think it might be easy, but i can't get the results i'm looking for.

-Matt

bmcgee

7:59 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



Select sTitle, Sum(iQuantity) AS Qty From OrderItem Group By sTitle Order By Qty DESC

This will give you how many items are on the most orders (which could also be a slightly different measure of "most popular"):
Select sTitle, Count(iQuantity) AS Qty From OrderItem Group By sTitle Order By Qty DESC

mattglet

10:06 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bmcgee-

i may have explained my needs incorrectly, but your statement did do the trick, exactly as i wanted. thanks a bunch for your help!

(i was putting my group by on the wrong column)

-Matt