How to get Count of each Column value of table?

Get count of each value in table column

  • I'm using SQL Server 2008 and I have a table that has a 'Status' column in it. There are five possible statuses: Submitted, In Progress, Deleted, Implemented, Not Implemented. I'm trying to figure out to write a SQL Statement that will return the count of each of these statuses but my code is either returning the number statuses, or the count of a specific status. Here's what I was working with: SELECT COUNT(ServiceAreaID) AS ServiceArea FROM Suggestions WHERE (ServiceAreaID = 6) or this one SELECT COUNT(DISTINCT ServiceAreaID) AS ServiceArea FROM Suggestions

  • Answer:

    For one: SELECT Status, COUNT(*) FROM dbo.Suggestions WHERE ServiceAreaID = 6 GROUP BY Status; For all: SELECT ServiceAreaID, Status, COUNT(*) FROM dbo.Suggestions GROUP BY ServiceAreaID, Status;

Omni at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

select ServiceAreaID,count(*),status from suggestion group by status,suggestion;

Habib.OSU

SELECT ServiceAreaID, COUNT(*) AS ServiceArea FROM Suggestions GROUP BY ServiceAreaID That should give you a count of each service area id. Replace the service area id in the group by with whatever your status is(below includes a status column seperate from serviceAreaID if that is what is needed). SELECT ServiceAreaID, status, COUNT(*) AS count FROM Suggestions GROUP BY ServiceAreaID, status

jzworkman

I think what you're looking for is something like this: SELECT ServiceAreaID, COUNT(ServiceAreaID) AS COUNT FROM Suggestions GROUP BY ServiceAreaID I didn't see a Status column in your SQL examples, but in case there's supposed to be one, the query would be more like this: This answer is basically identical to Aaron's, so if this is what you're looking for make sure to mark Aaron as the answer. SELECT ServiceAreaID, Status, COUNT(*) AS COUNT FROM Suggestions GROUP BY ServiceAreaID, Status

James Johnson

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.