Link to home
Start Free TrialLog in
Avatar of Richiep86
Richiep86

asked on

Joining 2 CTe together in ORACLE SQL

HI - me again,

i have the 2 following CTE i need to join together:

with
cte_base
as
(
select 
e.PERSON_REF,
e.EMPLOYEE_NUMBER,
coalesce(rtrim(ltrim(known_as)),per.FIRST_FORNAME)  as FORENAME,
per.SURNAME as SURNAME,
per.NI_NO NINO,
per.INITIALS INITS,
per.TITLE TITLE,
post.number_R as POST_NUMBER,
post.long_desc as POST_LONG_DESC,
e.START_DATE EMPSTARTDATE,
e.END_DATE EMPENDDATE,
CGrp.ID CGRPID,
CGrp.LONG_DESC CGRPDESC,
PGrp.ID PGRPID,
PGrp.LONG_DESC PGRPDESC,
narr_occtype.long_desc as OCC_TYPE,
narr_joinreason.long_desc as POST_JOIN_REASON,
narr_joinreason2.long_desc as EMP_JOIN_REASON,
ph.projected_end_date
from D550M e
left outer join D500M per on e.PERSON_REF = per.PERSON_REF
left outer join D100M CGrp on e.LEVEL1_PAY_STR_REF = CGrp.REF
left outer join D100M PGrp on e.PAY_STR_REF = PGrp.REF 
left outer join D580M ph on e.PERSON_REF = ph.PERSON_REF and
( ph.start_date <= sysdate and (ph.end_date is null or ph.end_Date >= sysdate))
left outer join D200M post on ph.ref  = post.ref
left outer join D800M narr_occtype on ph.occ_type = narr_occtype.narrative_code and narr_occtype.narrative_category = 'OCCTYP'
left outer join D800M narr_joinreason on ph.join_reason= narr_joinreason.narrative_code and narr_joinreason.narrative_category = 'RSNCHG'
left outer join D800M narr_joinreason2 on e.orig_how_joined = narr_joinreason2.narrative_code and narr_joinreason2.narrative_category = 'RSNCHG'

),

CTE_PostGrade AS
(
select e.PERSON_REF,
Gr.NUMBER_R GRADE
from D550M e
left join D580M ph on e.PERSON_REF = ph.PERSON_REF
left join D554M phGr on phGr.PERSON_REF = ph.PERSON_REF and
phGr.POST_REF = ph.REF
left join D400M Gr on Gr.REF = phGr.REF
where ph.MAIN_FLAG = 'Y'
and e.START_DATE <= trunc(SYSDATE) and coalesce(e.END_DATE, trunc(SYSDATE)) >= trunc(SYSDATE)
and ph.START_DATE <= trunc(SYSDATE) and coalesce(ph.END_DATE, trunc(SYSDATE)) >= trunc(SYSDATE)
and phGr.START_DATE <= trunc(SYSDATE) and coalesce(phGr.END_DATE, trunc(SYSDATE)) >= trunc(SYSDATE)
)
select 
--PERSON_REF,
EMPLOYEE_NUMBER,
FORENAME as KNOWN_AS,
SURNAME,
TITLE,
POST_NUMBER,
POST_LONG_DESC,
EMPSTARTDATE,
EMPENDDATE,
CGRPID,
CTE_PostGrade.NUMBER_R GRADE,
CGRPDESC,
PGRPID,
PGRPDESC,
OCC_TYPE,
POST_JOIN_REASON,
EMP_JOIN_REASON,
projected_end_date
from 
cte_lv
CTE_PostGrade

WHERE
EMP_JOIN_REASON NOT IN ('contractor','FTC New Starter')
AND
EMPSTARTDATE = sysdate -7

Open in new window


I need to join these 2 CTE together, but wasnt sure what to join on?

If someone could point me in the right direction i would be greatful,

Many thanks,

Rich
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richiep86
Richiep86

ASKER

Yeah - those are the correct fields - thankyou!

Thank you Geert for this explanation.
cte_lv.EMPLOYEE_NUMBER = CTE_PostGrade.PERSON_REF
 AND
EMP_JOIN_REASON NOT IN ('contractor','FTC New Starter')
AND EMPSTARTDATE  >= sysdate - 7

Open in new window


CTE_POSTGRADE.PERSON_REF: Invalid identifier.

Any ideaas?
Invalid identifier means..the column which you are selecting/using doesn't exist in the table/query used..

as you stated CTE_POSTGRADE.PERSON_REF doesnot exist..
I thought CTE_POSTGRADE.PERSON_REF does exist?

How do you kno it doesn't exist based on what I'v posted?

Many thanks!
Speedy, fast, helpful.

Thank you very much.
in this system you can share points on multiple answers ...
just to let you know :)
Geeert! I was supposed to do that. I do apologise.

I'll request attention and see if I can distribute the points across multiple solutions.

You helped me alot, it's only right I do the same. Plus, I have several more questions I'd like your insight on!

Rich
lol, don't bother, it was only a reminder