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)
 Query Help

Author  Topic 

nachi
Starting Member

7 Posts

Posted - 2005-08-17 : 14:12:32
Could some one help me in the following query

I have a table with the following structure

id Monyear type value
-- ------- ---- -----

1 jan2005 A Y
2 jan2005 A Y
3 jan2005 B N
4 jan2005 B Y
5 feb2005 A N


I need a query with the following results for each month and for all types in that month, I need the count of Y and Count of N on the value field. Value field will not have values other than 'Y' or 'N'

Result should be


Monyear type count_Y count_N

jan2005 A 2 0
jan2005 B 1 1
feb2005 A 0 1

Kristen
Test

22859 Posts

Posted - 2005-08-17 : 14:23:35
[code]
SELECT Monyear, type,
SUM(CASE WHEN value = 'Y' THEN 1 ELSE 0 END) AS [count_Y],
SUM(CASE WHEN value = 'N' THEN 1 ELSE 0 END) AS [count_N]
FROM MyTable
GROUP BY Monyear, type
[/code]
Kristen
Go to Top of Page

nachi
Starting Member

7 Posts

Posted - 2005-08-17 : 14:53:09
Thanks for the reply. I also got it right.
Go to Top of Page
   

- Advertisement -