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 2008 Forums
 Transact-SQL (2008)
 how to sum of two tables value

Author  Topic 

asifbhura
Posting Yak Master

165 Posts

Posted - 2012-03-04 : 00:42:55
Hello

I have two tables for rxample, t1 and t2.

both tables are having same columns name (monthno, amount)

i want to sum up amount group by monthno.( some time a month no might be available in either table only).

Regards

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-04 : 08:10:50
You can do a full outer join on the two tables and add up the amounts. Alternatively, you can do a UNION on the two sums as below:
SELECT
monthno,
SUM(amount) AS amount
FROM
(
SELECT
monthno, SUM(amount) AS amount
FROM
t1
GROUP BY
monthno
UNION ALL
SELECT
monthno, SUM(amount) AS amount
FROM
t2
GROUP BY
monthno
) s
GROUP BY
monthno
ORDER BY
monthno;
Go to Top of Page

asifbhura
Posting Yak Master

165 Posts

Posted - 2012-03-05 : 00:37:32
thanks buddy
Go to Top of Page
   

- Advertisement -