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
 Count & Max Issues

Author  Topic 

ddog6785
Starting Member

2 Posts

Posted - 2010-04-09 : 19:50:05
Hello everyone, I am having some problems with two questions. Here they are:

Which product is ordered most frequently?
For this one I would like to do something like
SELECT PRODUCT_ID, COUNT(PRODUCT_ID), FROM ORDER_LINE_T HAVING MAX(COUNT(PRODUCT_ID));
But that doesn't work and I can not do the top 1 because it has multiple of the same numbers.
Which product has the highest quantity ordered?
Basically, I assume this one uses the same logic.

Thank You for any help!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-09 : 21:42:51
[code]
select top 1
PRODUCT_ID, COUNT(PRODUCT_ID)
from ORDER_LINE_T
group by PRODUCT_ID
order by COUNT(PRODUCT_ID) desc
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-10 : 07:12:14
[code]select top 1 WITH TIES
PRODUCT_ID, COUNT(PRODUCT_ID) AS OrdCnt
from ORDER_LINE_T
group by PRODUCT_ID
order by OrdCnt desc
[/code]

if you have multiple products that match maximum ordered value

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -