Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wrote the query as
select id,count(id),flag from mytable group by id


but i get the result in single column

my table is
ID | flag
1  | 0
2  | 0
3  | 1
2  | 0
1  | 0
3  | 1
2  | 0
1  | 1
2  | 1
3  | 1


i want the report as this
ID | Count Flag O | Count Flag 1
1  |    2         | 1
2  |    3         | 1
3  |    0         | 3



please help me... thnks in advance :)
Posted

Select ID, SUM(case when flag = 0 then 1 else 0 end) as [Count Flag O], SUM(case when flag = 1 then 1 else 0 end) as [Count Flag 1] from test group by ID
 
Share this answer
 
Comments
CPallini 21-May-14 4:02am    
Damn, you beat me!
5.
SQL
SELECT  ID,
        SUM(IF(`flag` = 0, 1, 0)) 'Count Flag 0',
        SUM(IF(`flag` = 1, 1, 0)) 'Count Flag 1'
FROM    MyTable
GROUP   BY ID;
 
Share this answer
 
Comments
pradnyaa 21-May-14 4:12am    
thanks it worked :)
You can use case statement for achieving this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900