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

sql query help 2

Status
Not open for further replies.

MikeT

IS-IT--Management
Feb 1, 2001
376
0
0
US
Is it possible to create query which will report how many instances of a column values appear in the table?

Lets say I've a got a table with a column of states. I'd like to create a query that has a line for each state found in the table along with *how many* times it was found in the table.

Like
SELECT DISTINCT(statename)FROM states
would give me the name, but I also need to know how many times each state appears in the table.

Is this possible?
 
SELECT DISTINCT(statename),Count(statename) FROM states


Thanks

J. Kusch
 
Code:
SELECT statename, COUNT(*)
FROM states
GROUP BY statename
ORDER BY statename

--James
 
Sorry ...

SELECT DISTINCT(statename) as StateName,
Count(statename) as CountFoundForState
FROM states
GROUP BY statename

Thanks

J. Kusch
 
FYI, you don't need to use DISTINCT here as you will automatically get the distinct states by virtue of the GROUP BY.

--James
 
Thanks guys, those did the trick!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top