views:

336

answers:

3

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.
+3  A: 
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
Quassnoi
How to get the employee (suppose 5 actions) in a single row from latest to last actions with corresponding effdts, but not the last 5 rows from the job
Sorry, I don't quite get what you want. Please post some sample data and desired result in your question.
Quassnoi
He wants: emplid, effdt1, act1, effdt2, act2, effdt3, act3, ...; this is not a particularly good idea, of course.
Jonathan Leffler
True, but this the client requirement. And I need your help so complete the task.
See updated post.
Quassnoi
A: 
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
A: 

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.

PeopleSoftTipster