sql按月总计和逐年总计

时间:2019-09-07 17:44:11

标签: sql oracle group-by

RDBMS-最新的Oracle

我不在这里了。我需要按帐户和月份来组织帐户交易信息,还需要使用另一列来显示年初至今的汇总交易。这是我想要得到的东西的描述

ACCT_ID | ACCT_MM  | FISCAL_YYYY | FISCAL_MM_AMT | YTD_AMT
------------------------------------------------------------
    1   |    11    |     2018    |      25       |   100
    1   |    12    |     2018    |      50       |   150
    1   |    01    |     2019    |      20       |   20

我知道您可以通过ACCT_MM,FISCAL_YYYY与组一起获得FISCAL_MM_AMT 到目前为止,这就是我所想的全部。

SELECT ACCT_ID,ACCT_MM,FISCAL_YYYY,SUM(NVL(ACCT_TRNSCTN_AMT,0))
FROM TBL_ACCT_DETAIL
GROUP BY ACCT_ID,ACCT_MM,FISCAL_YYYY

现在如何将其与附加列YTD_AMT结合起来,使该年到本月为止的所有总计加起来令人困惑。 sql noob ftw。

3 个答案:

答案 0 :(得分:1)

按以下方式尝试分析函数SUM

SELECT T.*,
SUM(FISCAL_MM_AMT) 
OVER (PARTITION BY ACCT_ID, FISCAL_YYYY
ORDER BY ACCT_MM) AS YTD_AMT
FROM
(SELECT ACCT_ID,ACCT_MM,FISCAL_YYYY,SUM(NVL(ACCT_TRNSCTN_AMT,0)) AS FISCAL_MM_AMT
FROM TBL_ACCT_DETAIL
GROUP BY ACCT_ID,ACCT_MM,FISCAL_YYYY);

干杯!

答案 1 :(得分:0)

您可以使用累计和窗口功能:

SELECT ACCT_ID, ACCT_MM, FISCAL_YYYY,
       COALESCE(SUM(ACCT_TRNSCTN_AMT), 0),
       SUM(SUM(ACCT_TRNSCTN_AMT)) OVER (PARTITION BY ACCT_ID, FISCAL_YYYY ORDER BY ACCT_MM) AS YTD
FROM TBL_ACCT_DETAIL
GROUP BY ACCT_ID, ACCT_MM, FISCAL_YYYY

答案 2 :(得分:0)

您是否还想再增加一列,以将年份的总和与年初至今?

OLAP功能是先决条件。但是,每个受人尊敬的RDBMS都应该在此时提供这些功能。

然后,我认为(加上我认为应该是输入内容)...您应该去:

WITH
---- this is just the input so I have example data
input(     acct_id,acct_mm,fiscal_yyyy,fiscal_mm_amt) AS (
          SELECT 1,      1,       2018,            5 
UNION ALL SELECT 1,      3,       2018,            5
UNION ALL SELECT 1,      4,       2018,            5
UNION ALL SELECT 1,      5,       2018,           10
UNION ALL SELECT 1,      6,       2018,           10
UNION ALL SELECT 1,      7,       2018,           10
UNION ALL SELECT 1,      8,       2018,           10
UNION ALL SELECT 1,      9,       2018,           10
UNION ALL SELECT 1,     10,       2018,           10
UNION ALL SELECT 1,     11,       2018,           25
UNION ALL SELECT 1,     12,       2018,           50
UNION ALL SELECT 1,     01,       2019,           20
)
---- end of input -----
SELECT
  *
, SUM(fiscal_mm_amt) OVER(
    PARTITION BY acct_id,fiscal_yyyy
  ) AS fiscal_yy_amt
, SUM(fiscal_mm_amt) OVER(
    PARTITION BY acct_id,fiscal_yyyy 
    ORDER BY acct_mm
  ) AS ytd_amt
FROM input;

-- out  acct_id | acct_mm | fiscal_yyyy | fiscal_mm_amt | fiscal_yy_amt | ytd_amt 
-- out ---------+---------+-------------+---------------+---------------+---------
-- out        1 |       1 |        2018 |             5 |           150 |       5
-- out        1 |       3 |        2018 |             5 |           150 |      10
-- out        1 |       4 |        2018 |             5 |           150 |      15
-- out        1 |       5 |        2018 |            10 |           150 |      25
-- out        1 |       6 |        2018 |            10 |           150 |      35
-- out        1 |       7 |        2018 |            10 |           150 |      45
-- out        1 |       8 |        2018 |            10 |           150 |      55
-- out        1 |       9 |        2018 |            10 |           150 |      65
-- out        1 |      10 |        2018 |            10 |           150 |      75
-- out        1 |      11 |        2018 |            25 |           150 |     100
-- out        1 |      12 |        2018 |            50 |           150 |     150
-- out        1 |       1 |        2019 |            20 |            20 |      20
-- out (12 rows)
-- out 
-- out Time: First fetch (12 rows): 76.235 ms. All rows formatted: 76.288 ms