Table Self join - sql

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

Related

Covert left join to simple join

My LEFT JOIN subquery is taking longer. How do I covert it to JOIN?
SELECT * FROM "TABLE_A"
LEFT OUTER JOIN TABLE_B ON TABLE_A.mainid = TABLE_B.secondID
LEFT JOIN (
SELECT secondID
FROM TABLE_C
WHERE thirdID = 21
) Z ON TABLE_A.mainid = Z.secondID
WHERE "TABLE_A"."TRUEID" = 9
AND ((TABLE_A.USERNO IN (23))
First, write the query more concisely:
SELECT *
FROM "TABLE_A" a LEFT JOIN
TABLE_B b
ON a.mainid = b.secondID LEFT JOIN
TABLE_C c
ON a.mainid = c.secondID AND c.thirdID = 21
WHERE a."TRUEID" = 9 AND a.USERNO IN (23);
Then consider the indexes:
Table_A(TRUEID, USERNO, mainid)
Table_B(secondID)
Table_C(secondID, thirdID)

How to join a total of 6 tables?

I want join 6 tables:
Table A join Table B
Table A join Table C
Table A Join Table D
Table D Join Table E
Table E Join Table F
But I can't run it:
SELECT
A.ID, A.Name, A.SName,
B.tel, B.status,
C.ComCode, C.Comdes,
D.Type,
E.Des, E.AreaDes,
F.AreaID
FROM
((((Table A
INNER JOIN
Table B ON A.ID = B.ID)
INNER JOIN
Table C ON A.Concode = C.Comcode)
INNER JOIN
Table D ON A.Type = D.Type)
INNER JOIN
Table E ON D.Des = E.Des)
INNER JOIN
Table F ON E.AreaID = F.AreaID
Get rid of your parentheses and table and it should be vailid SQL, so
SELECT ...
FROM a INNER JOIN b ON a.a = b.a
INNER JOIN c ON b.b = c.a
...
Try this :
SELECT
A.ID, A.Name, A.SName,
B.tel, B.status,
C.ComCode, C.Comdes,
D.Type,
E.Des, E.AreaDes,
F.AreaID
FROM Table A
INNER JOIN Table B ON A.ID = B.ID
INNER JOIN Table C ON A.Concode = C.Comcode
INNER JOIN Table D ON A.Type = D.Type
INNER JOIN Table E ON D.Des = E.Des
INNER JOIN Table F ON E.AreaID = F.AreaID
It will help if you tell us what you database server is e.g., SQL Server, Oracle, MySQL, etc. When you say, "...I can't run it..." are you getting an error? If so, can you share that.
I ditched your parenthesis...
SELECT
A.ID,
A.Name,
A.SName,
B.tel,
B.status,
C.ComCode,
C.Comdes,
D.Type,
E.Des,
E.AreaDes,
F.AreaID
FROM
TableA A INNER JOIN TableB B ON A.ID = B.ID
INNER JOIN TableC C ON A.Concode = C.Comcode
INNER JOIN TableD D ON A.Type = D.Type
INNER JOIN TableE E ON D.Des = E.Des
INNER JOIN TableF F ON E.AreaID = F.AreaID
For clarity, I adjusted your table names (so it was clear what was Table A vs. Table B).

Controlling join order in combining left join and inner join in Impala SQL

With this kind of data
create table table_a as select 1 as id1;
insert into table_a values (2),(3);
create table table_b as select 1 as id1, 'a' as id2;
insert into table_b values (1,'b');
create table table_c as select 'a' as id2;
I have the following kind of join in Impala sql:
select *
from table_a as a
left join table_b as b
on b.id1 = a.id1
left join table_c as c
on c.id2 = b.id2
yielding this result
"id1","id1","id2","id2"
1,1,b,
1,1,a,a
2,,,
3,,,
I would like the second join to be inner join instead of left join:
select *
from table_a as a
left join table_b as b
on b.id1 = a.id1
join table_c as c /* <- How to process this join first without using inner queries? */
on c.id2 = b.id2
and get this result:
"id1","id1","id2","id2"
1,1,a,a
2,,,
3,,,
Thus, I would like the inner join of table_b and table_c to take place first and only after to do the left join between table_a and (table_b inner joined to table_b).
Is possible to determine the join order in such manner without using inner queries?
With the help from #jarlh, I realized left-to-right processing of joins and then found that it is possible to use RIGHT joins:
select *
from table_c as c
join table_b as b
on b.id2 = c.id2
right join table_a as a
on a.id1 = b.id1;
in order to get the desired result:
"id2","id1","id2","id1"
a,1,a,1
,,,2
,,,3

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 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'