I have 4 tables called A, B, C, C_History. Below is what is each table contains. cust_id is the common identifier for all tables.
A - cust_id,status,cust_type
B - cust_id,acnt_nmbr,acnt_systm
C - cust_id,email,mod_date
C_History - cust_id,email,mod_date(Same as C, used to insert rows which were deleted/updated in C).
I need to write a query combing all above tables to pull A.cust_id, B.acnt_nmbr,C.email(As New Email),C_History(As Old Email).
Caveat here is that, A and B tables will have only one record for a cust_id, but C and C_History can have multiple email records for a cust_id.
So from C table, i just need single email which was latest by using mod_date (TIMESTAMP datatype),
And from C_Hisory also, i just need single latest email using mod_date, but one more condition is that, whichever latest email picked from C_History, should not be same as the latest email picked from C.
Please help with this as i am banging my head from 2 days trying to figure this out.
Thank you legends in Advance.
SELECT A.cust_id
,B.acnt_nmbr
,C.email
,C_History.email
FROM A
,B
,(
SELECT cust_id
,MAX(email) KEEP (
DENSE_RANK LAST ORDER BY mod_date
) OVER (PARTITION BY cust_id)
FROM c
) c
,(
SELECT cust_id
,MAX(email) KEEP (
DENSE_RANK LAST ORDER BY mod_date
) OVER (PARTITION BY cust_id)
FROM c_history
) c_history
WHERE a.cust_id = b.cust_id
AND c_history.cust_id = a.cust_id
AND c.cust_id = a.cust_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.