0

従業員をフェッチする方法、たとえば、有効な日付のレコードにある最新の 5 つの Action_reason 行をフェッチする方法は、現在の行と履歴の行 (有効な日付 <= sysdate) のみを選択する必要があります。これらを 1 行で取得できますか、それとも従業員の場合は 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.
4

3 に答える 3

4
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
于 2009-03-03T17:03:57.273 に答える
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
于 2009-03-03T18:04:18.143 に答える