Reputation: 1
I have three tables with below data
Table A
JOB_NAME APP_NAME START_TIME
A ABC 00:00
C ABC 00:00
C ABCD 00:30
Table B
JOB_NAME APP_NAME SLA
A ABC 01:00
B ABC 01:00
C ABC 01:00
C ABCD 01:30
Table C
JOB_NAME APP_NAME PARENT
A ABC N/A
B ABC A
C ABC B
C ABCD N/A
I need to join these tables and generate below output.
JOB_NAME APP_NAME PARENT START_TIME SLA
A ABC N/A 00:00 01:00
B ABC A N/A 01:00
C ABC C 00:00 01:00
But the query I wrote returns below output.
JOB_NAME APP_NAME PARENT START_TIME SLA
A ABC N/A 00:00 01:00
C ABC B 00:00 01:00
Please help to fix this issue.
SELECT C.JOB_NAME,C.APP_NAME,C.PARENT,NVL(A.START_TIME,'N/A') AS START_TIME,B.SLA FROM C
LEFT JOIN B
ON UPPER(C.JOB_NAME) = UPPER(B.JOB_NAME)
LEFT JOIN A ON UPPER(C.JOB_NAME) = UPPER(A.JOB_NAME) WHERE C.APP_NAME='ABC' AND C.APP_NAME=A.APP_NAME AND C.APP_NAME=B.APP_NAME
Upvotes: 0
Views: 74
Reputation: 15991
Try this:
select c.job_name
, c.app_name
, c.parent
, nvl(a.start_time,'N/A') as start_time
, b.sla
from c
left join b
on upper(b.job_name) = upper(c.job_name)
and b.app_name = c.app_name
left join a
on upper(a.job_name) = upper(c.job_name)
and a.app_name = c.app_name
where c.app_name = 'ABC'
You have WHERE
clause conditions on A that require every row to have a value, and this excludes A.
I have always found that laying out code neatly makes issues like this easier to spot.
Upvotes: 0
Reputation: 30765
You've duplicated your JOIN logic in your WHERE clause:
ON UPPER(C.JOB_NAME) = UPPER(B.JOB_NAME)
...
WHERE ... AND C.APP_NAME=A.APP_NAME AND C.APP_NAME=B.APP_NAME
thereby turning your OUTER JOINs into INNNER ones. Get rid of the superfluous WHERE clause part, and you should be fine.
BTW: for your sample data, the UPPER
conversion is unnecessary; get rid of it if possible (since it will render indices on the JOB_NAME
columns useless)
Upvotes: 3