Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
HelloI 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 amountFROM( SELECT monthno, SUM(amount) AS amount FROM t1 GROUP BY monthno UNION ALL SELECT monthno, SUM(amount) AS amount FROM t2 GROUP BY monthno) sGROUP BY monthnoORDER BY monthno;