2012-12-05 49 views
0

我想要顯示的輸出是總EMPL_NUM的累加器。例如,該值顯示在字段TOTAL_FEBRUARY = TOTAL_JANUARY + TOTAL_FEBRUARY中,而該值存在於字段TOTAL_MARCH = TOTAL_MARCH + TOTAL_JANUARY + TOTAL_FEBRUARY中。我希望你們中的一些人可以提供解決方案。非常感謝你。編碼是如下顯示:如何累加器所有的值

SELECT 
(CASE WHEN To_Char(A.EFFDT,'MM')=01 THEN 1 
    WHEN To_Char(A.EFFDT,'MM')=02 THEN 2 
    WHEN To_Char(A.EFFDT,'MM')=03 THEN 3 
    WHEN To_Char(A.EFFDT,'MM')=04 THEN 4 
    WHEN To_Char(A.EFFDT,'MM')=05 THEN 5 
    WHEN To_Char(A.EFFDT,'MM')=06 THEN 6 
    WHEN To_Char(A.EFFDT,'MM')=07 THEN 7 
    WHEN To_Char(A.EFFDT,'MM')=08 THEN 8 
    WHEN To_Char(A.EFFDT,'MM')=09 THEN 9 
    WHEN To_Char(A.EFFDT,'MM')=10 THEN 10 
    WHEN To_Char(A.EFFDT,'MM')=11 THEN 11 
    WHEN To_Char(A.EFFDT,'MM')=12 THEN 12 
    ELSE NULL END) AS MONTHS 

    ,Count(*) AS EMPL_NUM 

    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=01 THEN 1 ELSE 0 END) AS TOTAL_JANUARY 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=02 THEN 1 ELSE 0 END) AS TOTAL_FEBRUARY 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=03 THEN 1 ELSE 0 END) AS TOTAL_MARCH 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=04 THEN 1 ELSE 0 END) AS TOTAL_APRIL 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=05 THEN 1 ELSE 0 END) AS TOTAL_MAY 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=06 THEN 1 ELSE 0 END) AS TOTAL_JUN 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=07 THEN 1 ELSE 0 END) AS TOTAL_JULY 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=08 THEN 1 ELSE 0 END) AS TOTAL_AUGUST 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=09 THEN 1 ELSE 0 END) AS TOTAL_SEPTEMBER 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=10 THEN 1 ELSE 0 END) AS TOTAL_OCTOBER 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=11 THEN 1 ELSE 0 END) AS TOTAL_NOVEMBER 
    ,Sum(CASE WHEN To_Char(A.EFFDT,'MM')=12 THEN 1 ELSE 0 END) AS TOTAL_DECEMBER 

    FROM PS_JOB A 
,PS_CITIZEN_PSSPRT B 
,PS_CITIZENSHIP C 
,PS_CITIZEN_STS_TBL D 

WHERE A.HR_STATUS='A' 

AND A.EFFDT=(SELECT Max(A1.EFFDT) FROM PS_JOB A1 WHERE A.EMPLID=A1.EMPLID AND A.EMPL_RCD=A1.EMPL_RCD AND A1.EFFDT<=SYSDATE) 

AND A.EMPL_RCD=0 

AND A.EFFSEQ=(SELECT Max(A2.EFFSEQ) FROM PS_JOB A2 WHERE A.EMPLID=A2.EMPLID AND A.EMPL_RCD=A2.EMPL_RCD AND A.EFFDT=A2.EFFDT) 

AND A.EMPLID =B.EMPLID(+) 

AND B.DEPENDENT_ID=' ' 

AND A.EMPLID=C.EMPLID 

AND B.EMPLID=C.EMPLID 

AND B.DEPENDENT_ID=C.DEPENDENT_ID 

AND B.COUNTRY=C.COUNTRY 

AND B.COUNTRY=D.COUNTRY          

AND C.COUNTRY=D.COUNTRY 

AND C.CITIZENSHIP_STATUS=D.CITIZENSHIP_STATUS 

AND C.CITIZENSHIP_STATUS IN ('5','7') 

AND To_Char(A.EFFDT,'YYYY')=2012 

    GROUP BY CASE WHEN To_Char(A.EFFDT,'MM')=01 THEN 1 
    WHEN To_Char(A.EFFDT,'MM')=02 THEN 2 
    WHEN To_Char(A.EFFDT,'MM')=03 THEN 3 
    WHEN To_Char(A.EFFDT,'MM')=04 THEN 4 
    WHEN To_Char(A.EFFDT,'MM')=05 THEN 5 
    WHEN To_Char(A.EFFDT,'MM')=06 THEN 6 
    WHEN To_Char(A.EFFDT,'MM')=07 THEN 7 
    WHEN To_Char(A.EFFDT,'MM')=08 THEN 8 
    WHEN To_Char(A.EFFDT,'MM')=09 THEN 9 
    WHEN To_Char(A.EFFDT,'MM')=10 THEN 10 
    WHEN To_Char(A.EFFDT,'MM')=11 THEN 11 
    WHEN To_Char(A.EFFDT,'MM')=12 THEN 12 
    ELSE NULL END 

NTER代碼在這裏

回答

2

使用< = SUM計算操作時,例如: -

Sum(CASE WHEN TO_NUMBER(To_Char(A.EFFDT,'MM')) <= 3 THEN 1 ELSE 0 END) AS TOTAL_MARCH 
+0

你好,謝謝你的建議。這是非常有用的。但是仍然有一個問題值得我關注,讓我們在2012年1月的時候表示,我希望在它之前顯示EMPL_NUM的總數,例如2011年12月的總數,我該如何做到這一點?介意給我一些建議呢?thxz〜 – goh6319

1

我想你問的是一個運行總和。看看分析功能(例如,psoug網站)。如果查詢的核心是正確的,則在內部查詢中進行常規聚合,然後將其包含在分析函數中以獲取運行總數。

SELECT month 
     ,SUM(month_count) OVER (ROWS BETWEEN UNBOUNDED PRECEDING 
             AND CURRENT ROW) 
     AS running_total 

FROM ( SELECT To_Char(A.EFFDT,'MM') AS month 
       ,COUNT(*) AS month_count 

      FROM PS_JOB A 
       ,PS_CITIZEN_PSSPRT B 
       ,PS_CITIZENSHIP C 
       ,PS_CITIZEN_STS_TBL D 

      WHERE A.HR_STATUS='A' 

      AND A.EFFDT=(SELECT Max(A1.EFFDT) 
          FROM PS_JOB A1 
          WHERE A.EMPLID=A1.EMPLID 
           AND A.EMPL_RCD=A1.EMPL_RCD 
           AND A1.EFFDT<=SYSDATE) 

      AND A.EMPL_RCD=0 

      AND A.EFFSEQ=(SELECT Max(A2.EFFSEQ) 
          FROM PS_JOB A2 
          WHERE A.EMPLID=A2.EMPLID 
           AND A.EMPL_RCD=A2.EMPL_RCD 
           AND A.EFFDT=A2.EFFDT) 

      AND A.EMPLID =B.EMPLID(+) 

      AND B.DEPENDENT_ID=' ' 

      AND A.EMPLID=C.EMPLID 

      AND B.EMPLID=C.EMPLID 

      AND B.DEPENDENT_ID=C.DEPENDENT_ID 

      AND B.COUNTRY=C.COUNTRY 

      AND B.COUNTRY=D.COUNTRY          

      AND C.COUNTRY=D.COUNTRY 

      AND C.CITIZENSHIP_STATUS=D.CITIZENSHIP_STATUS 

      AND C.CITIZENSHIP_STATUS IN ('5','7') 

      AND To_Char(A.EFFDT,'YYYY')=2012 

      GROUP BY To_Char(A.EFFDT,'MM') 

    ) 

ORDER BY month 
+0

感謝您的建議。這是非常有用的。但是仍然有一個問題值得我關注,讓我們在2012年1月的時候表示,我希望在它之前顯示EMPL_NUM的總數,例如2011年12月的總數,我該如何做到這一點?介意給我一些建議嗎?thxz〜 – goh6319