Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 COUNT

Author  Topic 

mukhan85
Starting Member

46 Posts

Posted - 2008-06-10 : 11:23:55
Hi, I have a problem with one query. I have a table that contains some records. Each record has a Status(possible values: Closed, completed, and Open) and Priority (possible values: from 0 to 4) I want to count by Priority, for example:

Status Open Completed Closed
0 3 2 1 <-- Number of records
1 10 22 12
2 ...
3
4

So, the first line says that there are 3 Open records which have Status 0.

Thank you.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-10 : 11:29:20
SELECT Priority,
SUM(CASE WHEN Status='Open' THEN 1 ELSE 0 END) AS Open,
SUM(CASE WHEN Status='Completed' THEN 1 ELSE 0 END) AS Completed,
SUM(CASE WHEN Status='Closed' THEN 1 ELSE 0 END) AS Closed
FROM YourTable
GROUP BY Priority
Go to Top of Page

mukhan85
Starting Member

46 Posts

Posted - 2008-06-10 : 11:39:36
Thank you, I appreciate your help. Can you recommend me a good book that would help me in writing SQL Queries?
Thanks.
quote:
Originally posted by visakh16

SELECT Priority,
SUM(CASE WHEN Status='Open' THEN 1 ELSE 0 END) AS Open,
SUM(CASE WHEN Status='Completed' THEN 1 ELSE 0 END) AS Completed,
SUM(CASE WHEN Status='Closed' THEN 1 ELSE 0 END) AS Closed
FROM YourTable
GROUP BY Priority

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-10 : 11:42:45
quote:
Originally posted by mukhan85

Thank you, I appreciate your help. Can you recommend me a good book that would help me in writing SQL Queries?
Thanks.
quote:
Originally posted by visakh16

SELECT Priority,
SUM(CASE WHEN Status='Open' THEN 1 ELSE 0 END) AS Open,
SUM(CASE WHEN Status='Completed' THEN 1 ELSE 0 END) AS Completed,
SUM(CASE WHEN Status='Closed' THEN 1 ELSE 0 END) AS Closed
FROM YourTable
GROUP BY Priority




Books online should be a good source for writing sql queries.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-10 : 14:32:46
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-10 : 14:41:55
quote:
Originally posted by madhivanan

Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Madhivanan

Failing to plan is Planning to fail


Hi Madhi
Working late?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-10 : 14:43:38
quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Madhivanan

Failing to plan is Planning to fail


Hi Madhi
Working late?


No. I am outside India
Where are you now?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-10 : 14:46:27
quote:
Originally posted by madhivanan

quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Madhivanan

Failing to plan is Planning to fail


Hi Madhi
Working late?


No. I am outside India
Where are you now?

Madhivanan

Failing to plan is Planning to fail


I'm at home Doing daily catch up
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-10 : 14:50:27
quote:
Originally posted by madhivanan

No. I am outside India
Already leaving marriage?

Just kidding...



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-10 : 14:52:07
quote:
Originally posted by Peso

quote:
Originally posted by madhivanan

No. I am outside India
Already leaving marriage?

Just kidding...



E 12°55'05.25"
N 56°04'39.16"





Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -