Joining on multiple tables in teradata - sql

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'

Related

Oracle SQL: How to replace distinct with where exists

I've read that using a where exists clause could usually be more efficient than writing select distinct. How could I rewrite the below 2 queries using the where exists condition? Not sure if query2 is eligible for this clause or this only applies to joins.
Query 1:
SELECT DISTINCT
e.field1,
regexp_substr(substr(TRIM(d.field2), 1, 2), '[A-Za-z]+', 1, 1) postal_group
FROM
table1 e
JOIN table1 f ON f.field0 = e.field0
JOIN table2 g ON g.field3 = f.field3
JOIN table3 g ON g.field4 = f.field4
JOIN table4 a ON a.field5 = g.field5
JOIN table5 b ON ( b.field6 = a.field6
AND b.field7 = a.field7 )
JOIN table6 c ON ( c.field8 = b.field8
AND c.field9 = b.field9 )
JOIN table7 d ON ( d.field10 = c.field10
AND d.field11 = c.field11 )
Query 2:
SELECT DISTINCT
field
FROM
table1
WHERE
condition = 'value'
For your second query you can use group by clause to avoid DISTINCT.
SELECT field
FROM
table1
WHERE
condition = 'value'
Group by field
Please try below query with where exists instead of Distinct:
SELECT
e.field1,
regexp_substr(substr(TRIM(d.field2), 1, 2), '[A-Za-z]+', 1, 1) postal_group
FROM
table1 e
where exists
(
select 1 from table1 f
JOIN table2 g ON g.field3 = f.field3
JOIN table3 g ON g.field4 = f.field4
JOIN table4 a ON a.field5 = g.field5
JOIN table5 b ON ( b.field6 = a.field6
AND b.field7 = a.field7 )
JOIN table6 c ON ( c.field8 = b.field8
AND c.field9 = b.field9 )
JOIN table7 d ON ( d.field10 = c.field10
AND d.field11 = c.field11 )
where f.field0 = e.field0
)
You can't avoid a join with d because you need a column from it. Therefore the best you can do is:
select e.column1
, d.column2 as postal_group
from table1 e
join table7 d
on d.column10 = c.column10
and d.column11 = c.column11
where exists
( select 1 from table1 f
join table2 g on g.column3 = f.column3
join table3 h on h.column4 = f.column4
join table4 a on a.column5 = h.column5
join table5 b on b.column6 = a.column6 and b.column7 = a.column7
join table6 c on c.column8 = b.column8 and c.column9 = b.column9
where f.column0 = e.column0 );
You only need distinct if table7.column2 can have duplicate values for a (column10, column11) combination.
This may or may not be more efficient than the original version - compare timings, execution plans, reads etc.
(I've renamed 'fields' as columns because that's what tables have. Also there are no brackets in a join clause and adding them tends to confuse code formatters.)

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?).

Consolidating 5 Queries into One

I have two pieces of data that I have analysed and found five distinct ways in which the data is linked. Is it possible to combine these into one query? Combining queries like these is outside my skill set, but getting these queries into a singular one would be a huge help for what I am trying to accomplish.
Here are the queries (Actual Table and Column names have been replaced)
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table3 C ON B.Col4=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table5 E ON E.Col14=B.Col4 AND Col7='Value1'
LEFT JOIN Table6 F ON E.Col8=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table7 E ON E.Col10=B.Col4 AND Col7='Value2'
LEFT JOIN Table6 F ON E.Col11=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 C;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table7 F ON F.Col10=b.Col4 AND Col7='Value3'
LEFT JOIN Table3 C ON F.Col11=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table8 E ON E.Col12=B.Col4 AND Col7='Value4'
LEFT JOIN Table6 F ON E.Col13=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;
The result of the queries should give data like below. For any value in Col1 there could be multiple values in Col2, however, for each Col1/Col2 pairing, only 1 set of the queries above creates the link between the two entities.
Col1 | Col2
-----------
1 | A
2 | B
2 | C
3 | D
4 | A
Thank you for any assistance. Let me know if have any questions on the queries or results.
Note - These queries are being executed against an Oracle database.
Use:
SELECT a.Col1, d.Col2
FROM Table2 B
LEFT JOIN Table1 A ON A.Col1=B.Col3
LEFT JOIN Table5 H ON H.Col14=B.Col4 AND Col7='Value1'
LEFT JOIN Table7 E ON (E.Col10=B.Col4 AND Col7='Value2') OR (F.Col10=b.Col4 AND Col7='Value3')
LEFT JOIN Table6 F ON (E.Col13=F.Col9) OR (E.Col8=F.Col9) OR (E.Col11=F.Col9) OR (H.Col8=F.Col9)
LEFT JOIN Table3 C ON (B.Col4=C.Col5) OR (F.Col9=C.Col5) OR (F.Col11=C.Col5)
LEFT JOIN Table4 D ON D.Col2=C.Col6;
or a UNION operator for your select statements.

Adding more joins to a select

I am having some trouble trying to add 2 more joins to a select. The bellow works for me:
FROM
TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
LEFT JOIN TABLE3 C ON A.REQUESTED_BY = C.USER_NAME
LEFT JOIN TABLE3 D ON A.COORDINATOR = D.USER_NAME
INNER JOIN TABLE4 E ON A.ID = E.PARENT_ID
INNER JOIN TABLE5 F ON E.ID = F.ID
But I need to get more information, so I tried something like this (added the last 2 rows):
FROM
TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
LEFT JOIN TABLE3 C ON A.REQUESTED_BY = C.USER_NAME
LEFT JOIN TABLE3 D ON A.COORDINATOR = D.USER_NAME
INNER JOIN TABLE4 E ON A.ID = E.PARENT_ID
INNER JOIN TABLE5 F ON E.ID = F.ID
INNER JOIN TABLE6 G ON A.ID = B.ID
LEFT JOIN TABLE3 H ON G.COORDINATOR = H.USER_NAME
And this isn't working like it should.
Question: How can I add the last two joins to make the select works? Thanks.
You're not actually joining to TABLE6 (G) anywhere. I would think that this join:
INNER JOIN TABLE6 G ON A.ID = B.ID
should be something like this instead:
INNER JOIN TABLE6 G ON A.ID = G.ID
And as a side note, I hope you're using table aliases that are more meaningful than A, B, C, etc. in your real code. ;-)

Joining query Result with another table

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