Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Report Query

Author  Topic 

justynb
Starting Member

7 Posts

Posted - 2009-09-24 : 15:14:52
Hi everyone, quite new to SQL and having trouble getting my heaad around how to do something. I hope you can help.

I have a table called Orders for items that have been ordered through my website

I would like create a report that shows how many of each has been sold so I can see what is most popular, eg

White Widgets: 34
Brown Widgets: 22
Red Widgets: 10

In my table I have CustomerID (unique ID of customer) ProductID (unique ID of Product from the table Products) and OQuantity (the number of this product that this customer ordered.

SO what I need to do is somehow add up all the "OQuantity" for each "ProductID" then sort by total "Oquantity" DESC. I have no idea how to do something this complex though.

If anyone could assist I would be most grateful.

Thank you in advance.

Justyn.

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2009-09-24 : 16:19:18
You'll using SUM and a GROUP BY. Something like this...

SELECT O.ProductID, P.ProductName, SUM(O.Quantity) as TotalSold
FROM Orders as O
JOIN Products as P on O.ProductID = P.ProductID
GROUP BY O.ProductID, P.ProductName
ORDER BY 3 DESC

--------------------------------------------
Brand yourself at EmeraldCityDomains.com
Go to Top of Page

justynb
Starting Member

7 Posts

Posted - 2009-09-24 : 16:33:35
That's done the trick - thank you very much. The code you posted is so simple, I just couldn't picture how to do it myself.

Kind regards,

Justyn.
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2009-09-24 : 19:57:02
Glad to help.

--------------------------------------------
Brand yourself at EmeraldCityDomains.com
Go to Top of Page
   

- Advertisement -