15,997,043 members
See more:
Table 1:
SQL
```id amount
1   100
2   200
3   300
4   400```

Table 2:
SQL
```id   amount
1    100
1    100
2    200
3    300
4    null```

Table 3:
SQL
```id amount
1   null
2   200
2   200
3   300
3   200
4   null```

id is common for each tables , how can i get output like this:
SQL
```id  t1   t2   t3
1  100  200  null
2  200  200   400
3  300  300   500
4  400  null null```

i am stuck with this .
Posted
Updated 7-Dec-13 18:46pm
v6

## Solution 2

This will give you what you want. But I supposed that table1 has all the Ids.

SQL
```select id,  (select sum(amount) from table1 st1 where st1.id=tbl1.id) t1,
(select sum(amount) from table2 st2 where st2.id=tbl1.id) t2,
(select sum(amount) from table3 st3 where st3.id=tbl1.id) t3
from table1 tbl1```

Good Luck

## Solution 1

SQL
```SELECT
TBL1.AMOUNT T1,
TBL2.AMOUNT T2,
TBL3.AMOUNT T3
FROM TBL1
LEFT JOIN TBL2 ON TBL2.ID = TBL1.ID
LEFT JOIN TBL3 ON TBL3.ID = TBL1.ID```

Now after improving the question by OP, this solution is of course not the solution.

v5
King Fisher 7-Dec-13 7:05am
thanks,
i used this ,but im getting id which is repeats more than one time
[no name] 7-Dec-13 7:52am
But that means TBL1 (or one of the other two tables) contains the same ID more then one time.

In case all the tables contain ID unique, I'm pretty sure the select statement above is right.

So the question is what will you do if ID is not unique. Do you like then to have the respective sum of the amounts?
King Fisher 7-Dec-13 8:17am

i used this

SELECT
TBL1.ID,
sum(TBL1.AMOUNT T1),
sum(TBL2.AMOUNT T2),
sum(TBL3.AMOUNT T3)
FROM TBL1
LEFT JOIN TBL2 ON TBL2.ID = TBL1.ID
LEFT JOIN TBL3 ON TBL3.ID = TBL1.ID group by id

i got the unique id 1,2,3,4 , but the amount is not respective to the id
[no name] 7-Dec-13 8:44am
You improved the question. Are you sure the result table is correct? E.g. is it not like this that T3 for ID==2 should be 400?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 60 George Swan 10 Richard MacCutchan 10 Idle_Force -2 Dave Kreskowiak -2
 OriginalGriff 431 Pete O'Hanlon 225 Dave Kreskowiak 123 Richard Deeming 120 CPallini 110

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900