Oracle Rolling or Running totals by month

前端 未结 2 1337
走了就别回头了
走了就别回头了 2021-02-15 12:50

Goal: Rolling/Running total for all statements at the end of each month.

Code:

select 
    TRUNC(ACTHX.STMT_HX_STMT_DATE, \'MM\') AS MNTH, 
    COUNT(ACT         


        
相关标签:
2条回答
  • 2021-02-15 13:41

    An alternative solution that provides the same rolling totals without the sub query.

    SELECT
            DISTINCT TRUNC(ACTHX.STMT_HX_STMT_DATE,'MM') AS MNTH
            ,count(ACTHX.INVOICE) OVER (ORDER BY TRUNC(ACTHX.STMT_HX_STMT_DATE,'MM')) AS STMTS
        FROM  ACTHX
        ORDER BY
            TRUNC(ACTHX.STMT_HX_STMT_DATE,'MM')
    
    0 讨论(0)
  • 2021-02-15 13:48

    It sounds like you want the running total. You can use the analytic SUM function for that.

    select mnth,
           sum(stmts) over (order by mnth) stmts
      from (select TRUNC(ACTHX.STMT_HX_STMT_DATE, 'MM') AS MNTH, 
                   COUNT(ACTHX.INVOICE) as STMTS
              from ACTHX
             group by TRUNC(ACTHX.STMT_HX_STMT_DATE, 'MM'))
     ORDER BY mnth
    
    0 讨论(0)
提交回复
热议问题