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!

Query Question Top N? 1

Status
Not open for further replies.

shannanl

IS-IT--Management
Apr 24, 2003
1,071
US
I have a query that returns a sum of tests. This query returns all tests. I only need to return the top 10 tests in quantity. How can I do this?

Thanks in advance - Shannan

SELECT DISTINCT Procedures_T.P_num, Procedures_T.P_desc, SUM(Pat_ProcedureCharges_T.Proc_qty) AS TOTAL
FROM Procedures_T LEFT JOIN Pat_ProcedureCharges_T ON Procedures_T.Entry_id = Pat_ProcedureCharges_T.Proc_id
GROUP BY P_num, P_Desc
 
You don't need distinct because you are using Group By on the columns you are selecting. So just add a Top 10 clause:

Code:
SELECT TOP 10 Procedures_T.P_num, Procedures_T.P_desc, SUM(Pat_ProcedureCharges_T.Proc_qty) AS TOTAL
FROM Procedures_T LEFT JOIN Pat_ProcedureCharges_T ON Procedures_T.Entry_id = Pat_ProcedureCharges_T.Proc_id
GROUP BY P_num, P_Desc
ORDER BY SUM(Pat_ProcedureCharges_T.Proc_qty) DESC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top