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.
| 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 websiteI would like create a report that shows how many of each has been sold so I can see what is most popular, egWhite Widgets: 34Brown Widgets: 22Red Widgets: 10In 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 TotalSoldFROM Orders as OJOIN Products as P on O.ProductID = P.ProductIDGROUP BY O.ProductID, P.ProductNameORDER BY 3 DESC--------------------------------------------Brand yourself at EmeraldCityDomains.com |
 |
|
|
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. |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2009-09-24 : 19:57:02
|
| Glad to help.--------------------------------------------Brand yourself at EmeraldCityDomains.com |
 |
|
|
|
|
|