Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hai all

i have one table like

HTML
sid	name	subject	attendance
1	jhon	sub1	1
2	toni	sub1	0
3	danial	sub1	1
4	jafer	sub1	1
5	jhon	sub2	1
6	toni	sub2	1
7	danial	sub2	1
8	jafer	sub2	0

here subject names are dynamic we dont know about subject name before writing query i need output like
HTML
name	sub1	sub2
jhon	1	    1
toni	0	    1
danial	1	    1
jafer	1	    0

am unable to write query please help me
thanks in advance..
Posted
Updated 21-Jan-15 23:10pm
v4

1 solution

try with group by and case statement
SQL
select name, SUM(CASE WHEN subject = 'sub1' and  attendance = 1 THEN 1 ELSE 0 END) as sub1,
 SUM(CASE WHEN subject = 'sub2' and  attendance = 1 THEN 1 ELSE 0 END) as sub2
from Table1
group by name
 
Share this answer
 
Comments
tulasiram.v 22-Jan-15 5:09am    
but i dont know the subject names here it is dynamic one

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