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
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
Related Q & A:
- How to add a column header to the dynamic table?Best solution by stackoverflow.com
- Can I estimate the row count of a large mysql table using the disk space?Best solution by Database Administrators
- Can I get facebook unique share count?Best solution by Stack Overflow
- How to get the column value when a row is selected in wpf listview?Best solution by Stack Overflow
- How to find the column name of the first column with a null value?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.