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!

SQL counting help 2

Status
Not open for further replies.

tsyle

Programmer
May 16, 2005
35
US
I have 1 table Visit_Procedure. What I want to find is which procedure was ordered the most.

Visit_Number Procedure_Code Number_Of_Times
1 BDR 1
1 EEX 1
1 REX 1
2 EEX 1
2 REX 3
3 AEX 1
3 BDR 1
3 CXR 1

Answer should be REX 4

I have no idea where to start with this. I used
Select Max(Procedure_Code) but it not giving me the right asnwer. help...
 
SELECT TOP 1 Procedure_Code
FROM table
GROUP BY Procedure_Code
ORDER BY Sum(Number_Of_Times) DESC

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
I think this should work.


Select Top 1 Procedure_Code, Sum(Number_Of_Times) As OrderCount
From Visit_Procedure
Group By Procedure_Code
Order By Sum(Number_Of_Times) Desc

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
JohnD,

I swear I checked to see if someone else answered this prior to clicking the submit button.

At least our answers are similar.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Great minds think alike.

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top