Joining query Result with another table - sql

I have the following query
Select TA.Column1 , COALESCE(TE.Column2,TA.Column2) as Mydata
from TableA TA
INNER JOIN TableB TB ON (TA.Column2 =TB.Column1)
LEFT JOIN TableC TC ON (TB.Column2 = TC.Column1)
LEFT JOIN TableD TD ON (TC.Column1 = TD.Column1)
LEFT JOIN TableE TE ON(TD.Column2 = TE.Column1)
To get the result I am looking for I need to Join the MyData column with another TableX
e.g INNER JOIN TableX TX ON (TableX.Column1 = MyData) and have TableX.COlumn2 in my select query .
My query is how can I join the COALESCE(TE.Column2,TA.Column2) as Mydata fetched value with a table TableX

On the surface, you should be able to do this. (Did you try that already?)
Select TA.Column1 , COALESCE(TE.Column2,TA.Column2) as Mydata, TX.Column2
from TableA TA
INNER JOIN TableB TB ON (TA.Column2 =TB.Column1)
LEFT JOIN TableC TC ON (TB.Column2 = TC.Column1)
LEFT JOIN TableD TD ON (TC.Column1 = TD.Column1)
LEFT JOIN TableE TE ON(TD.Column2 = TE.Column1)
LEFT JOIN TableX TX ON (COALESCE(TE.Column2,TA.Column2)) = TX.Column1

Related

How to make a table left outer join of multiple tables

I am need to join a main table with two feed tables.Table data below:
MainTable
FeedTable
Query I am running is left inner join
Select ID, MT.ColumnA, FT1.ColumnA, FT1.ColumnB, FT2.ColumnA, FT2.ColumnC, FT3.ColumnA, FT3.ColumnD
from MainTable MT
Left Join FeedTable FTI1 on FT1.fk1 = MT.key
Left Join FeedTable FT2 on FT2.fk2 = MT.key
Left Join FeedTable FT3 on FT3.fk3 = MT.key
The output I get is :
The Output I want to get is :
I am assuming left outer join isn't the way to go about this or am I doing the join wrong?
You want the tables in the wrong order. The FeedTable should be first:
Select ft.*, mt1.key as cola, mt2.key as colb, mt3.key colc
from FeedTable ft left join
MainTable mt1
on ft.fk1 = mt1.key left join
MainTable mt2
on ft.fk2 = mt2.key left join
MainTable mt2
on ft.fk3 = mt3.key;

Table Self join

Table_a has columns : old_id and new_id . Following query gives planning region and organization code for old_id.
SELECT a.old_id,d.planning_region,b.organization_code
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
My requirement is get organization code for new_id too. So my output will be like this
old_id, planning_region ( of old_id ), organization_code (of old_id ) and organization_code (of new_id ).
Self Join should work but here in this case, Do I need to do self join of all 4 tables ?
Note: new_id also can be joined same as old_id with table_b.
If I am understanding correctly, you can add more joins.
If you just want the new organization_code:
SELECT
a.old_id,
d.planning_region,
b.organization_code,
b1.organization_code organization_code_new
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
INNER JOIN table_b b1 ON a.new_id = b1.organization_id
If you also want the planning_region, then we need to bring d as well:
SELECT
a.old_id,
d.planning_region,
b.organization_code,
d1.planning_region planning_region_new,
b1.organization_code organization_code_new
FROM table_a a
INNER JOIN table_b b ON a.old_id = b.organization_id
INNER JOIN table_c c ON c.organization_code = b.organization_code
INNER JOIN table_d d ON d.planning_location_id = b.organization_code
INNER JOIN table_b b1 ON a.new_id = b1.organization_id
INNER JOIN table_c c1 ON a.new_id = c1.organization_id
INNER JOIN table_d d1 ON d1.planning_location_id = b1.organization_code
Side note: it is not obvious what the purpose of table c is in the query (apart, maybe, filtering?).

Joining on multiple tables in teradata

Please help me through this
sel a.col1,a.co2,a.col3,.........b.col1,b.col2..,c.col1,c.col2
from table1 as a inner join table2 as b on a.col1 =b.col1
inner join table3 as c on a.col1 = b.col1
where col1 = xxxxx;
Now i need join one more table table4. As table4 dont have col1 as primary index in it I need to join this to another table which has Primary key.
The below is the different query which i need inculde this in to the above sel statement.
Sel xx.col1,yy.aaa,yy.bbb,zz.ccc,zz.ddd,zz.eee
from tablea as xx, tableb as yy, table4 as zz
where xx.col1 = yy.bbb and yy.aaa = zz.ccc
Primary indexs :
col1 for table1,table2,table3,tablexx
aaa for tableb
ccc for table4
Thanks in advance
How about:
Select a.leg,c.btn,p.prods,svc.sr,speed.test, a.leg, b.acct_id, e.emp_no, e.emp_name
FROM db1.tb1 as a
inner join db1.tb2 as C ON a.leg = C.leg
inner join db1.tb3 as p ON a.leg = p.leg
inner join db1.tb3 as svc on a.leg = svc.leg
inner join db2.tb4 as speed on a.leg = speed.leg
inner join db4.tb1 as b on a.leg = b.sce_acct_id
inner join db4.tb5 as e on b.acct_id = e.acct_id
where a.leg ='xxxx'

Inner Joining three tables

I have three tables I wish to inner join by a common column between them.
Say my tables are;
TableA TableB TableC
I wish to join A-B, but then also B-C all by this common field I will call common.
I have joined two tables like this;
dbo.tableA AS A INNER JOIN dbo.TableB AS B
ON A.common = B.common
How do I add the third one?
select *
from
tableA a
inner join
tableB b
on a.common = b.common
inner join
TableC c
on b.common = c.common
Just do the same thing agin but then for TableC
SELECT *
FROM dbo.tableA A
INNER JOIN dbo.TableB B ON A.common = B.common
INNER JOIN dbo.TableC C ON A.common = C.common
dbo.tableA AS A INNER JOIN dbo.TableB AS B
ON A.common = B.common INNER JOIN TableC C
ON B.common = C.common
try the following code
select * from TableA A
inner join TableB B on A.Column=B.Column
inner join TableC C on A.Column=C.Column
try this:
SELECT * FROM TableA
JOIN TableB ON TableA.primary_key = TableB.foreign_key
JOIN TableB ON TableB.foreign_key = TableC.foreign_key

Difficulty with sql query

I have the following tables:
TableA (id, tableB_id, tableC_id)
TableB (id, expirationDate)
TableC (id, expirationDate)
I want to retrieve all the results from TableA ordered by tableB.expirationDate and tableC.expirationDate. How can I do this?
select ta.*
from TableA ta
inner join TableB tb on ta.tableB_id = tb.id
inner join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
Update:
If you are not getting all the records, then you'll need to use a left outer join:
select ta.*
from TableA ta
left outer join TableB tb on ta.tableB_id = tb.id
left outer join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
If the result set is empty with the other suggestions, are you sure the data in the tables is actually correctly correlated to each other?
Can you post some sample rows for each table?
Have you tried:
SELECT a.* FROM TableA a
INNER JOIN TableB b on b.id = a.tableB_id
INNER JOIN TableC c on c.id = a.tableC_id
ORDER BY b.expirationDate, c.expirationDatetableB_id