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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Get 2 Counts in 1 SQL?

Author  Topic 

matkwan
Starting Member

36 Posts

Posted - 2007-05-08 : 09:52:31
Hi,

Here is my 'Message' table:
UserID, Message, Read
1,'AAA',1
1,'BBB',1
1,'CCC',1
1,'DDD',0
1,'EEE',0

I would like to return the count of total messages belonging to UserID = 1 and also the count of total Unread messages belonging to UserID = 1. For example, total 5 messages and 2 unread messages.

Is it possible to achieve this in 1 SQL?

Matt

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-08 : 10:01:22
[code]Select
UserID,
count(Message) as TotalCount,
Sum(case when Read = 1 then 1 else 0 end) as ReadCount,
Sum(case when Read = 0 then 1 else 0 end) as UnreadCount
From Table
Group by UserID
[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -