Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have an SQL table with a column called Town, it looks something like this

 

Burnley

Burnley

Burnley

Blackburn

Preston

Colne

Colne

Preston

Blackburn

 

I need to get the counts of each town in the database so I want my results to look like this

 

Burnley 3

Blackburn 2

Preston 2

Colne 2

 

Is there anyway I can do it just using a plain SQL statement?

 

Thanks

Posted

Yes, this can be done with COUNT() function and GROUP BY statement

 

SELECT COUNT(name) AS count, name FROM table GROUP BY name 

 

Where 'table' is name of the table and column 'name' is the name of the column you use.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...