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.
Author |
Topic |
king_fisher
Starting Member
13 Posts |
Posted - 2013-12-07 : 06:58:34
|
TAble 1:id amount1 1002 2003 3004 400 Table 2:id amount1 1001 1002 2003 3004 null Table 3:id amount1 null2 2002 2003 3003 2004 null id is common for each tables , how can i get output like this:Collapse | Copy Codeid t1 t2 t31 100 200 null2 200 200 2003 300 300 5004 400 null nulli m stuck with this query .vijay nelson |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-07 : 08:29:38
|
[code]SELECT t1.id,t1.TotalAmount,t2.TotalAmount,t3.TotalAmountFROM (SELECT id,SUM(amount) AS TotalAmt FROM Table1 GROUP BY id )t1INNER JOIN (SELECT id,SUM(amount) AS TotalAmt FROM Table2 GROUP BY id )t2ON t2.id = t1.idINNER JOIN (SELECT id,SUM(amount) AS TotalAmt FROM Table3 GROUP BY id )t3ON t3.id = t1.id[/code]If all tables doesnt have all id values then use FULL JOIN rather than INNER JOIN------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|