SQL queries for multiple columns depends on same column of another table
-
See the following scenario: Tables for the question:
Table : T1
c10 c12
e1 A
e2 B
e3 CTable: T2
c1 c2 c3 c4 c5 C6
1 P e1 e2 null X
2 Q e3 null null Y
3 R e2 e1 null X
4 S e3 e1 e2 ZThe c3, c4 and c5 columns of T2 table depends on c12 column of T1 table.
I want the following output:
c1 c2 c3 c4 c5
1 P A B null
2 Q C null null
3 R B A null
4 S C A BCan you help me to write an SQL to retrieve the above output from the table T1 and T2? The SQL may be like:
Select c1, c2, t1.c12 as c3....... from t1, t2 where join condition
But I am not getting the actual sql in my mind. So please help me regarding this.
-
See the following scenario: Tables for the question:
Table : T1
c10 c12
e1 A
e2 B
e3 CTable: T2
c1 c2 c3 c4 c5 C6
1 P e1 e2 null X
2 Q e3 null null Y
3 R e2 e1 null X
4 S e3 e1 e2 ZThe c3, c4 and c5 columns of T2 table depends on c12 column of T1 table.
I want the following output:
c1 c2 c3 c4 c5
1 P A B null
2 Q C null null
3 R B A null
4 S C A BCan you help me to write an SQL to retrieve the above output from the table T1 and T2? The SQL may be like:
Select c1, c2, t1.c12 as c3....... from t1, t2 where join condition
But I am not getting the actual sql in my mind. So please help me regarding this.
I think it would be something like this:
SELECT c1, c2, tbl1.c12 AS c3, tbl2.c12 AS c4, tbl3.c12 AS c5
FROM
T2 LEFT OUTER JOIN
T1 AS tbl1 ON T2.c3 = tbl1.c10 LEFT OUTER JOIN
T1 AS tbl2 ON T2.c4 = tbl2.c10 LEFT OUTER JOIN
T1 AS tbl3 ON T2.c5 = tbl3.c10Jack of all trades ~ Master of none.
-
See the following scenario: Tables for the question:
Table : T1
c10 c12
e1 A
e2 B
e3 CTable: T2
c1 c2 c3 c4 c5 C6
1 P e1 e2 null X
2 Q e3 null null Y
3 R e2 e1 null X
4 S e3 e1 e2 ZThe c3, c4 and c5 columns of T2 table depends on c12 column of T1 table.
I want the following output:
c1 c2 c3 c4 c5
1 P A B null
2 Q C null null
3 R B A null
4 S C A BCan you help me to write an SQL to retrieve the above output from the table T1 and T2? The SQL may be like:
Select c1, c2, t1.c12 as c3....... from t1, t2 where join condition
But I am not getting the actual sql in my mind. So please help me regarding this.
Here it is
select t2.c1,t2.c2,t1.c12 as c3,t11.c12 as c4,t111.c12 as c5
from table2 t2
inner join table1 t1 on t1.c10 = t2.c3
left join table1 as t11 on t11.c10 = t2.c4
left join table1 as t111 on t111.c10 = t2.c5
I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.