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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem w/ Parameter Value? 1

Status
Not open for further replies.

NFLDUser

IS-IT--Management
Apr 17, 2006
47
CA
I have a table called PatientList with a field called 'city'. I want to find the TOP 10 cities for all rows. Here's my code:

SELECT TOP 10 city, newvar=Count(*)
FROM PatientList
GROUP BY city
ORDER BY newvar ASC

But I keep getting asked for a value for newvar. But newvar is supposed to be a new variable. What am I doing wrong?
 
SELECT TOP 10 city, Count(*) AS newvar
FROM PatientList
GROUP BY city
ORDER BY 2 DESC

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, that works perfect.

How could I go one step beyond that and also display a field that shows what percentage each city represents overall?
 
One way:
SELECT TOP 10 city, Count(*) AS newvar
, (Count(*)/(SELECT Count(*) FROM PatientList)) AS [%]
FROM PatientList
GROUP BY city
ORDER BY 2 DESC

Another way:
SELECT TOP 10 P.city, Count(*) AS newvar
, (Count(*)/T.overall) AS [%]
FROM PatientList AS P, (
SELECT Count(*) AS overall FROM PatientList
) AS T
GROUP BY P.city, T.overall
ORDER BY 2 DESC

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top