Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure - duplication 1

Status
Not open for further replies.

ClassyLinks

Programmer
Sep 13, 2001
8
CA
Hi all!

I've got a stored procedure that is not behaving...or perhaps it is behaving, I just haven't trained it correctly.

What I'm TRYING to do is have a list of our top 10 selling items.

What is happening is that if an item sells more than once, it appears multiple times in the top 10 list.

You can see what I mean here: (check out the "Most Popular Items" list towards the bottom of the page.

The stored procedure I'm using is:

SELECT TOP 10
Orders.OrderDate,
Orders.OrderID,
OrderDetails.ProductID,
SUM(OrderDetails.Quantity) as TotalNum,
Products.ModelName,
Products.ThemeDesigner

FROM
(OrderDetails
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID)
INNER JOIN Orders ON Orders.OrderID = OrderDetails.OrderID

WHERE OrderDetails.UnitCost > 1
AND datediff(day,Orders.OrderDate,getdate()) < 7
AND OrderDetails.OrderDownloaded = '1'

GROUP BY
Orders.OrderID,
Orders.OrderDate,
OrderDetails.ProductID,
Products.ModelName,
Products.ThemeDesigner

ORDER BY
Orders.OrderDate DESC, TotalNum DESC



Any ideas?? How can I get each item to appear only once? The select should be the OrderDetails.ProductID....I've tried DISTINCT, but that didn't work.

Any help would be greatly appreciated.

Cheers!

ClassyLinks ClassyLinks :cool:
 
don't show results at the order level, summarize over all orders

SELECT TOP 10
SUM(OrderDetails.Quantity) as TotalNum,
Products.ModelName,
Products.ThemeDesigner

FROM
(OrderDetails
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID)
INNER JOIN Orders ON Orders.OrderID = OrderDetails.OrderID
WHERE OrderDetails.UnitCost > 1
AND datediff(day,Orders.OrderDate,getdate()) < 7
AND OrderDetails.OrderDownloaded = '1'
GROUP BY
Products.ModelName,
Products.ThemeDesigner

ORDER BY
SUM(OrderDetails.Quantity) DESC

rudy
 
Thanks Sooooooooo Much!

That did the trick!

Cheers! ClassyLinks :cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top