Nested Table Joins - sql

I have C table as Main Table, which is joining left join with E table on col3 and which is joining left join with F table on col4.
(
FROM A
LEFT OUTER JOIN B ON A.col1 = B.col1
LEFT OUTER JOIN C ON A.col2 = C.col2
LEFT OUTER JOIN E ON A.col3 = E.col3
LEFT OUTER JOIN F ON A.col4 = F.col4
)temptab1
Which needs to left join with
(
FROM C ON
LEFT OUTER JOIN E ON C.col3 = E.col3
LEFT OUTER JOIN F ON C.col4 = F.col4
)temptab2
Now, should i join temptab1 to temptab2 on (C.Col3 = temptab2.Col3 OR C.Col4 = temptab2.Col4) ? OR
should i join temptab1 to temptab2 on (C.Col3 = temptab2.Col3 AND C.Col4 = temptab2.Col4) ?
IF i have to use OR clause, it is hindering performance of the query a lot...
I tried using union clause instead of OR clause. Still, query performance is not improving...
Please suggest better query.

The temptab1 is all rows of A. The temptab2 is all rows of C. And the temptab1 is LEFT JOIN with temptab2, which results in all rows of A.
You may use CASE statements to identify the matching values across A, B, C, D, E, F and remove the Join with temptab2.
Apologies, could not comment...

Related

BiqQuery Left Outer with an OR condition Error

Ran into the BigQuery limitation that you can't use an OR on a JOIN. Runs fine in Oracle.
Looking for some hints on how to accomplish this in another matter.
The last left outer join produces this error.
LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
select [col list]
from tablea a
left outer join tableb b on a.id = b.ID
left outer join tabled d on a.id = d.iid
left outer join tablee e on b.id = e.pid and b.cid = e.id
left outer join tablef f on d.runitid = f.id or e.runitid = f.id
Due to additional join dependencies down the stack I switched it to a JOIN which allows the OR condition. More verification of results will be done.

Oracle inner and outer join valid combinations

Is it valid to combine Oracle inner and outer joins in the same query a la:
select b.col1, c.col2, sum(d.col1), sum(e.col1) from
a
inner join b on a.xxx = b.xxx
inner join c on a.yyy = c.yyy
left join d on b.aaa = d.aaa and c.bbb = d.bbb
left join e on b.aaa = e.bbb and c.aaa = e.bbb
group by b.col1, c.col2
Yes, you can combine inner, left and right joins the way you deem fit in the query. Just make sure that you understand the implications of inner, left and outer joins. Couple of blogs describe joins nicely: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ and http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
The outer join nulls are ignored in your sum(d.col1) and sum(e.col1) values.
Why SUM(null) is not 0 in Oracle?
If alternate default values are needed for your null outer join results, consider NVL and similar.
Sum columns with null values in oracle
Please try this, All non aggregated columns should be in group by clause.
select b.col1, c.col2, sum(d.col1), sum(e.col1) from
a
inner join b on a.xxx = b.xxx
inner join c on a.yyy = c.yyy
left join d on b.aaa = d.aaa and c.bbb = d.bbb
left join e on b.aaa = e.bbb and c.aaa = e.bbb
group by b.col1, c.col2

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

How to write a transitive sql join in DB2?

I want something like the following.
SELECT fewCols, aColFromNewTbl FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B ON A.ID = B.ID
LEFT OUTER JOIN TABLE_C AS C ON A.ID = C.ID
INNER JOIN A_NEW_TABLE AS NEWTBL ON NEWTBL.ID = B.ID;
Somehow I'm not able to achieve this functionality. Actually above query is suppose to join A with NEWTBL, but I'm joining it with B, which is already joined with A. For my results I want them to come exclusively from the join of NEWTBL and B. I don't know how I can get desired results?
Probably you need this:
SELECT fewCols, aColFromNewTbl
FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B
INNER JOIN A_NEW_TABLE AS NEWTBL
ON NEWTBL.ID = B.ID
ON A.ID = B.ID
LEFT OUTER JOIN TABLE_C AS C
ON A.ID = C.ID;

Cascading left outer joins

What is the correct syntax to perform an outer join with the following requirements:
A left outer join B on A.c1 = B.c1
B left outer join C on B.c2 = C.c2
A left outer join D on A.c1 = D.c1
So A, B, and C cascade and A and D cascade.
I know how to write the A->B->C but I don't know how to add D. I need scope or parenthesis or something.
this should work as you want:
SELECT
*
FROM A
left outer join B on A.c1 = B.c1
left outer join C on B.c2 = C.c2
left outer join D on A.c1 = D.c1
the DB engine looks at what your are joining to, not the order of the joins. D joins to A, and has nothing to do with B or C
The order in which you join doesn't matter, the database will build a result set of every combination of rows in all tables, limited by the on clause. For example, because 1=1 is always true, this would give you 1000 rows:
select *
from ten_row_table A
left join ten_row_table B on 1=1
left join ten_row_table C on 1=1
But this would give you 10 rows:
select *
from ten_row_table A
left join ten_row_table B on A.id = B.id
left join ten_row_table C on A.id = C.id
You can make complicated queries slightly more readable by indentation. We indent second and further dependencies by four spaces, like:
from A
left outer join B on A.c1 = B.c1
left outer join C on B.c2 = C.c2
left outer join D on C.c3 = D.c3
left outer join E on B.c2 = E.c2
left outer join F on A.c1 = F.c1