Link to home
Start Free TrialLog in
Avatar of ValleyENT
ValleyENT

asked on

SQL Column Count

Hey All,

Trying to write a simple query that will look in my table dbo.Profile count the total number of entries in 'City', then get a count for each city found.

Example:

Total = 140,000

Peoria = 150 entrie(s)
Ajo = 1 entrie(s)
Glendale = 9000 entrie(s)

Is this possible?
Avatar of chapmandew
chapmandew
Flag of United States of America image

select city, count(*) as numberofcities
from profile
group by city
ASKER CERTIFIED SOLUTION
Avatar of chapmandew
chapmandew
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will do
SELECT city, count(*) AS cnt
FROM dbo.Profile
GROUP BY city
WITH rollup 

Open in new window

ValleyENT,

Assuming City is a column in the table Profile, you can execute the following query to get the results you are looking for

select City, count(*) from Profile group by City

if you really want the "Total" as well you could so something like this

select 'Total', count(*) from Profile
union all
select City,Count(*) from Profile group by City

Avatar of ValleyENT
ValleyENT

ASKER

How would I include the corrosponding State if the column name was STATE?
SELECT City, COUNT(City) as "Total City(s)"
FROM dbo.PatientProfile
GROUP BY City ORDER By City

Open in new window

do you want it included w/ the city information or a new query just for state information?
included please.
Select City, State, count(City) as "Total Cities" from dbo.PatientProfile group by City,State order by City, State