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
 top 10 used parts?

Author  Topic 

jaystar
Starting Member

12 Posts

Posted - 2012-06-18 : 09:40:15
Hi all I have a table that has Part Numbers in and qyt used, I am after a query which will show me the most commonly used part number.

This is my attempt and failed

--first 10 orders
select top 10 *
from SLADB.dbo.PartsUsedTB
order by Qty

I realise I must group and sum somewhere ?

jaystar
Starting Member

12 Posts

Posted - 2012-06-18 : 09:48:38
select top 10
Part_Number

,SUM(Cost)

from
SLADB.dbo.PartsUsedTB


group by
Part_Number
Go to Top of Page

jaystar
Starting Member

12 Posts

Posted - 2012-06-18 : 09:58:45
SELECT qty, Part_Number, sum(Cost)
FROM SLADB.dbo.PartsUsedTB
GROUP BY qty, Part_Number
ORDER BY qty, sum(Cost) desc
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-18 : 12:08:09
it should be

SELECT Part_Number
FROM SLADB.dbo.PartsUsedTB
GROUP BY Part_Number
ORDER BY SUM(qty) DESC


depending on how many results you want you can apply TOP N over it (Top 10, Top 50 etc)

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

Go to Top of Page
   

- Advertisement -