使用有效的日期记录

发布于 2024-07-14 01:52:54 字数 358 浏览 6 评论 0原文

如何获取员工,例如有效日期记录中的 5 个最新 Action_reason 行,没有未来行,应仅选择当前行和历史行(有效日期 <= sysdate)。 我可以在单行中获取这些数据,还是对于 Employee 来说它是 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.

How to fetch an Employees, say 5 latest Action_reason rows which are in an effective dated record, no future rows, should select only current and history rows(effective date <= sysdate). Can I fetch these in single row or will it be 5 rows for an Employee?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2024-07-21 01:52:54
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
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
呆° 2024-07-21 01:52:54
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
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
季末如歌 2024-07-21 01:52:54

您可以按照您希望的方式获取数据。 如果您希望它为五行,那么您可以使用以下内容:

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。

You can have the data any way you wish. If you want it as five rows then you could use this:

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

If you want the data all on a single line then use Oracle's LAG analytic function, thus:

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)

This gives the last 5 effdt values and the last 5 action reason values. If you don't need both the above SQL can be trimmed accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文