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

select distinct and how many

Status
Not open for further replies.

sevex

Programmer
Sep 18, 2001
74
CA
Is it possible to do a SELECT DISTINCT, to select all distinct records, but also select how many of each distinct record there are?

table data like:
1, cat
2, dog
3, cat
4, dog
5, dog

and get a recordset with:
cat, 2
dog, 3

thanks in advance for any help.
 
oh i just thought of it.

distinct(field) as mydistinctfield, count(field) as fieldcount from mytable group by mydistinctfield?

I can't test it out yet cause I'm waiting for datasources...
 
Hello Sevex,

You gave yourself the answer already, but why not create the table from your example (with the 5 records) and check it out. The principle is correct or not regardless if you apply it to 5 or 100.000 records from a source.
Be aware that this counting only works if you only select 1 field to count. Select 2 fields and you will aggregate over these two fields.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top