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
 General SQL Server Forums
 New to SQL Server Programming
 SLQ union with sum

Author  Topic 

krishna_yess
Yak Posting Veteran

81 Posts

Posted - 2009-10-21 : 08:55:23
i have table like this
State ----------------------		a	b	c	d
-----------------------------------------------------------------
Active --------------------- 0 0 0 0
Assigned ---------------- 0 0 0 0
submitted --------------- 0 0 0 0
Under_Investigation ----- 0 0 0 0


and

 State ----------------------		a	b	c	d
-----------------------------------------------------------------
Active --------------------- 0 69 7 5
Assigned ---------------- 8 0 9 7
submitted --------------- 0 77 0 8
Under_Investigation ----- 9 0 0 9


and i want the result like this

State ----------------------		a	b	c	d
-----------------------------------------------------------------
Active --------------------- 0 69 7 5
Assigned ---------------- 8 0 9 7
submitted --------------- 0 77 0 8
Under_Investigation ----- 9 0 0 9


is this possible in SQl using union or other

thanks

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-10-21 : 09:50:26
Do you have one or two tables? If you have one
SELECT State,[a] = sum(a),[b] = sum(b),[c] = sum(c),[d] =sum(d)
FROM yourTable
GROUP BY State

If you have two tables
SELECT State,[a] = sum(a),[b] = sum(b),[c] = sum(c),[d] =sum(d)
FROM
(select State,a,b,c,d
from yourTable
UNION ALL
select State,a,b,c,d
from yourOtherTable
) t

GROUP BY State

Jim





Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -