如何积累所有价值观

时间:2012-12-05 02:23:40

标签: sql accumulator accumulate

我要显示的输出是总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 

这里是代码

2 个答案:

答案 0 :(得分:2)

计算SUM时使用&lt; =运算符,例如:

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

答案 1 :(得分: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