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)
 Joining three transaction table

Author  Topic 

kaushals
Starting Member

5 Posts

Posted - 2011-06-16 : 14:22:54
I have three tables.
table 1
id type
aaa 1
bbb 1
ccc 1

table 2
ddd 2
eee 2
aaa 2

table 3
fff 3
bbb 3
ddd 3

Output would be :
aaa 1 2 null
bbb 1 null 3
ccc 1 null null
ddd null 2 3
eee null 2 null
fff null null 3

How can I get this output? I am lost on this one. There is no master table so I don't have any base to compare to transaction tables.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-16 : 14:33:50
You can do a full join as in:

select
coalesce(t1.id,t2.id,t3.id) as ID,
t1.[type],
t2.[type],
t3.[type]
from
Table1 t1
full join Table2 t2 on t1.id = t2.id
full join Table3 t3 on t3.id = t1.id
Go to Top of Page
   

- Advertisement -