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 |
sura
Starting Member
10 Posts |
Posted - 2012-03-22 : 00:46:47
|
Hello AllI have two tables like below. This is something like parent child relationship. Table 1 have relation details and Table 2 have ids for each row. Table 1Col1 Col2 col3A A1 A1.1B B1 B1.1B B1 B1.2C C1 C1.1C C2 C2.1C C3 C3.1Table 2Col1 KEYA 1A1 2A1.1 3B 4B1 5B1.1 6B1.2 7C 8C1 9C1.1 10C2 11C2.1 12C3 13C3.1 14Extecpted outputCHILD PARENT14 1312 1110 913 811 89 87 56 55 43 22 1NULL 1I am not good to write this as a query and it would be great if any one can help me in this.ThanksSURA |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2012-03-22 : 14:53:08
|
[CODE]select distinct c2.Key Child, p2.Key Parent -- Gather Col1, Col2 key combinationsfrom Table1 t1inner join Table2 p2 on t1.Col1 = p2.Col1inner join Table2 c2 on t1.Col2 = c2.Col1union allselect distinct c2.Key, p2.Key -- Gather Col2, Col3 key combinationsfrom Table1 t1inner join Table2 p2 on t1.Col2 = p2.Col1inner join Table2 c2 on t1.Col3 = c2.Col1union allselect null, 1 -- For no discernable reason[/CODE]=================================================It is not so much our friends' help that helps us as the confident knowledge that they will help us. -Epicurus, philosopher (c. 341-270 BCE) |
 |
|
sura
Starting Member
10 Posts |
Posted - 2012-03-22 : 23:19:54
|
Thanks Bustaz Kool.Its really cool.RegardsSURA |
 |
|
|
|
|