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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 help with sp

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2003-08-13 : 21:29:47
I have a table with 50,000 records. In this table is a column called STATEID (1 - 65) (some provinces too).

I want to have a query that comes up with the count of rows for each stateid. Do I have to write a query for each stateid ?

ie

select count(userID) as '1' from tbluserdetails where stateprovid = '1'
select count(userID) as '2' from tbluserdetails where stateprovid = '2'


Or is there a better, simpler, faster way?

Thanks for any help

Mike123

chadmat
The Chadinator

1974 Posts

Posted - 2003-08-13 : 21:35:13
select StateProvID, count(*)
from tbluserdetails
group by StateProvID

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-08-13 : 21:46:01
Chad has it right, but you might want to name the column with the count..

SELECT StateProvID , count(*) as Total

FROM tbluserdetails

GROUP BY StateProvID

ORDER BY StateProvID -- or you might ORDER BY Total


Sam
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2003-08-15 : 04:56:00
thanks alot guys

always a great help


mike123

Go to Top of Page
   

- Advertisement -