Click here to Skip to main content
15,889,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi frd'z.............................
i have a table like.......................

c.id	gas	water	wire	dranage	roadl	roadtype
1	100	100	0	0	100	a
1	25	0	51	55	250	b
1	0	0	12	0	150	c
1	60	80	0	70	50	d
2	50	0	20	50	10	a
2	50	70	0	0	50	b
2	0	80	0	20	20	c
2	0	0	10	50	30	d
.	.	.	.	.	.	.
.	.	.	.	.	.	.
n	n	n	n	n	n	n


------------------------------------------------------------------------------------------------
i wnat result like........

select when gas=0.00 then sum(roadl)as b,when water=0.00 then sum(roadl)as c,when wire=0.00 then sum(roadl)as d,when dranage=0.00 then sum(roadl)as a from table

cid	b	c 	d	a	
1	150	400	150	250	
2	50	40	70	50


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 15-Mar-13 21:01pm
v5
Comments
Sandeep Mewara 15-Mar-13 7:20am    
Ok. And what have you tried? Where are you stuck?
rahul 92 15-Mar-13 8:17am    
i haven't any idea to do this
Sandeep Mewara 16-Mar-13 3:50am    
It does not work like this here.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.
gvprabu 15-Mar-13 7:51am    
Give some more clear explanation about your problem...
rahul 92 15-Mar-13 8:16am    
i have a table which is declare alrady
i want to sum of roadl which parallel row those column value which have 0.00 value in a seperate column ........

exa. is decalared in my problem..................

1 solution

You need to use a CASE WHEN clause.
Try this:
SQL
SELECT cid,
       SUM(CASE WHEN gas=0 THEN roadl ELSE 0 END) AS b,
       SUM(CASE WHEN water=0 THEN roadl ELSE 0 END) AS c,
       SUM(CASE WHEN wire=0 THEN roadl ELSE 0 END) AS d,
       SUM(CASE WHEN dranage=0 THEN roadl ELSE 0 END) AS a
FROM MyTable GROUP BY cid
 
Share this answer
 
Comments
Maciej Los 16-Mar-13 11:07am    
Excellent!
+5!

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