我已經嘗試了很多google-fu,但無法完全找到我正在尋找的解決方案。大多數對我的情況來說太高級了。我也是SQL的新手,所以我對這個問題的新問題表示歉意。SQL:僅返回包含字段更改的行
這是針對Oracle 10g的。
PS_JOB員工擁有唯一的EMPLID,但他們的FILE_NBR可以更改。我需要返回每個具有FILE_NBR更改的EE的EMPLID。這是一個較大的查詢的一小部分,它將PS_JOB與其他幾個表連接在一起,因此請詳細說明解決方案的位置:主選擇語句,連接或其他where子句。
對於PS_JOB的一個簡單的表格例子
EMPLID FILE_NBR Action Date
0005 12345 Hire 01/01/2013
0005 67890 term 04/05/2015
0006 55555 Hire 02/05/2014
0006 55555 term 04/15/2015
我想回到EMPLID 0005,因爲它有一個FILE_NBR變化
ADDED編輯: 我想避免這樣做,因爲整個的目的查詢有點不同。整個查詢的目的是爲了返回在b.rehire_dt值電子工程師,沒有REH在a.ACTION_REASON的價值,確實有一個a.FILE_NBR變化
Select a.paygroup, a.EMPLID, a.FILE_NBR, c.name, e.TLM_STATUS, b.rehire_dt as "Emplmnt_Rltd_Dtes-Rehire Date",
CASE
WHEN a.ACTION_REASON = 'REH' THEN 'Y'
ELSE 'N'
END -- confirms if EE is true rehire
FROM PS_EMPLOYMENT b --no effective dated rows
right outer join SYSADM.PS_JOB a
on a.emplid = b.emplid
right outer join SYSADM.PS_PERSONAL_DATA c -- no effective dated rows
on a.emplid = c.emplid
right outer join SYSADM.PS_SMS_SUBSCRB_TBL d
on a.paygroup = d.SUBSCRIBER_ID
right outer join PORTAL.PS_TS_EMPL_TLM_STATUS e
on a.emplid = e.emplid
where
b.rehire_dt IS NOT NULL
--and a.EFFDT = (select max (a2.effdt) from SYSADM.PS_JOB a2 where
a.EMPLID = a2.EMPLID)
and d.EFFDT = (select max (d2.effdt) from PS_SMS_SUBSCRB_TBL d2 where
d.SUBSCRIBER_ID = d2.SUBSCRIBER_ID)
and e.EFFDT = (select max (e2.effdt) from PORTAL.PS_TS_EMPL_TLM_STATUS e2
where e.emplid = e2.EMPLID)
and d.EFF_STATUS <> 'A'
--and a.action <> 'REH'
--and a.ACTION_REASON <> 'REH'
--and b.rehire_dt = a.EFFDT
--and b.emplid = '50731/246'
order by a.paygroup, a.emplid
感謝您的幫助!
親切的問候, 克雷格
SOLUTION: 感謝@Adam_Martin 我幾乎只是從零開始,在select語句
SELECT distinct JOB.EMPLID, JOB.PAYGROUP, PER.NAME, EMP.REHIRE_DT as "Empl_Rlated_Dates_Rehire_Date", SUB.EFF_STATUS as "Paygroup Status",TLM.TLM_STATUS,
CASE
WHEN JOB.ACTION_REASON = 'REH' THEN 'Y'
ELSE 'N'
END as "True Rehire?", -- confirms if EE is true rehire
CASE -------------------------------Adam's code
WHEN EXISTS(SELECT 1
FROM PS_JOB otherJobs
WHERE otherJobs.EMPLID = JOB.EMPLID
AND otherJobs.FILE_NBR <> JOB.FILE_NBR) THEN 'Y'
ELSE 'N'
END as "Had FN Change?"
FROM SYSADM.PS_EMPLOYMENT EMP
INNER JOIN SYSADM.PS_JOB JOB
ON EMP.EMPLID = JOB.EMPLID
INNER JOIN SYSADM.PS_PERSONAL_DATA PER -- no effective dated rows
on JOB.emplid = PER.emplid
INNER JOIN SYSADM.PS_SMS_SUBSCRB_TBL SUB
on JOB.paygroup = SUB.SUBSCRIBER_ID
INNER JOIN PORTAL.PS_TS_EMPL_TLM_STATUS TLM
on JOB.emplid = TLM.emplid
WHERE
JOB.EMPLID = '50731/246'
and EMP.REHIRE_DT IS NOT NULL
and JOB.EFFDT = (select max (JOB2.effdt) from SYSADM.PS_JOB JOB2 where
JOB.EMPLID = JOB2.EMPLID)
and SUB.EFFDT = (select max (SUB2.effdt) from PS_SMS_SUBSCRB_TBL SUB2 where
SUB.SUBSCRIBER_ID = SUB2.SUBSCRIBER_ID and SUB2.EFF_STATUS = 'A')
請發表您的當前查詢。 –
如果你不顯示示例表/查詢來理解你要求的內容,沒有人會理解你在說什麼。你可以發佈你的連接,並在/ group/by /子句中,而不是select。你絕對需要給我們一些示例表。 –
完成..對不起,回合! –