如何获取一个Employees,比如5个最新的Action_reason行,这些行在有效的日期记录中,没有未来的行,应该只选择当前行和历史行(生效日期< = sysdate)。我可以在单行中获取这些内容,还是为员工提取5行?
select emplid, effdt, action_reasons
-- we have to build a logic here.
-- Should we initialize 5 ACT variables to fetch rows into it?
-- Please help
from JOB
where emplid = '12345'
and effdt <= sysdate.
答案 0 :(得分:3)
SELECT LTRIM(SYS_CONNECT_BY_PATH(emplid || ', ' || effdt || ', ' || action_reasons, ', '), ', ')
FROM (
SELECT
FROM (
SELECT emplid, effdt, action_reasons, ROW_NUMBER() OVER (ORDER BY effdt) AS rn
FROM JOB
WHERE emplid= '12345'
AND effdt <= SYSDATE
)
WHERE rn <= 5
)
WHERE CONNECT_BY_ISLEAF = 1
START WITH
rn = 1
CONNECT BY
rn = PRIOR rn + 1
答案 1 :(得分:0)
SELECT JOBXX.EMPLID,JOBXX.EFFDT,JOBXX.ACT1,JOBXX.ACT2,JOBXX.ACT3,JOBXX.ACT4,JOBXX.ACT5
FROM
(SELECT SD.EMPLID,
SD.EFFDT,
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 1 THEN SD.A1 ELSE 0 END)),1,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 1 THEN SD.A1 ELSE 0 END)),3,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 1 THEN SD.A1 ELSE 0 END)),5,2))
AS ACT1,
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 2 THEN SD.A1 ELSE 0 END)),1,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 2 THEN SD.A1 ELSE 0 END)),3,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 2 THEN SD.A1 ELSE 0 END)),5,2))
AS ACT2,
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 3 THEN SD.A1 ELSE 0 END)),1,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 3 THEN SD.A1 ELSE 0 END)),3,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 3 THEN SD.A1 ELSE 0 END)),5,2))
AS ACT3,
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 4 THEN SD.A1 ELSE 0 END)),1,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 4 THEN SD.A1 ELSE 0 END)),3,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 4 THEN SD.A1 ELSE 0 END)),5,2))
AS ACT4,
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 5 THEN SD.A1 ELSE 0 END)),1,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 5 THEN SD.A1 ELSE 0 END)),3,2)) ||
CHR(SUBSTR(TO_CHAR(SUM(CASE WHEN SD.R3 = 5 THEN SD.A1 ELSE 0 END)),5,2))
AS ACT5
FROM (
SELECT EMPLID,EFFDT,ACTION_REASON,
SUBSTR(ACTION_REASON,1,1),
SUBSTR(ACTION_REASON,2,1),
SUBSTR(ACTION_REASON,3,1),
TO_NUMBER(ASCII(SUBSTR(ACTION_REASON,1,1)) ||
ASCII(SUBSTR(ACTION_REASON,2,1)) ||
ASCII(SUBSTR(ACTION_REASON,3,1))) AS A1,
ROW_NUMBER() over(PARTITION BY EMPLID,EFFDT ORDER BY EFFDT desc,EFFSEQ desC) R3
FROM PS_JOB
WHERE action in ('ABC','XYZ')
and action_reason in ('123','456','789')
and emplid IN('12345','ABCDE')
AND effdt between '01-jan-2008' and '18-dec-2008'
ORDER BY EFFDT DESC, EFFSEQ DESC
) SD
GROUP BY EMPLID , EFFDT
) JOBXX
答案 2 :(得分:0)
您可以按照自己的方式获取数据。如果你想将它作为五行,那么你可以使用它:
select * from (
select emplid, empl_rcd, effdt, action_reason
, rank() over (partition by emplid, empl_rcd
order by effdt desc, effseq desc) rank1
from ps_job
where emplid = '12345'
and effdt <= sysdate)
where rank1 <= 5
如果您想将数据全部放在一行上,那么请使用Oracle的LAG分析函数,因此:
select * from (
select emplid, empl_rcd, effdt
, lag(effdt) over(partition by emplid, empl_rcd order by effdt, effseq) effdt_lag1
, lag(effdt, 2) over(partition by emplid, empl_rcd order by effdt, effseq) effdt_lag2
, lag(effdt, 3) over(partition by emplid, empl_rcd order by effdt, effseq) effdt_lag3
, lag(effdt, 4) over(partition by emplid, empl_rcd order by effdt, effseq) effdt_lag4
, action_reason
, lag(action_reason) over(partition by emplid, empl_rcd order by effdt, effseq) action_reason_lag1
, lag(action_reason, 2) over(partition by emplid, empl_rcd order by effdt, effseq) action_reason_lag2
, lag(action_reason, 3) over(partition by emplid, empl_rcd order by effdt, effseq) action_reason_lag3
, lag(action_reason, 4) over(partition by emplid, empl_rcd order by effdt, effseq) action_reason_lag4
from ps_job
where emplid = '12345') j
where effdt = (
select max(j1.effdt) from ps_job j1
where j1.emplid = j.emplid
and j1.empl_rcd = j.empl_rcd
and j1.effdt <= sysdate)
这给出了最后5个effdt值和最后5个动作原因值。如果您不需要,则可以相应地修剪上述SQL。