Home > Back-end >  Cumulative Sum Group By Month
Cumulative Sum Group By Month

Time:05-06

I need to do cumulative sum on one of the field(value column from above data) along with grouping it by month. Here is how my data looks:

enter image description here

I need the output as follows:

enter image description here

I am doing this in Impala, I tried couple of things, but it did not worked.

Thank you for your help!

CodePudding user response:

Since analytic functions are applied to the select list expressions after the aggregation, you may wrap aggregation sum with an analytic sum ordered by month:

with t(id, dt, val) as (
  select *
  from(values
    (1, date '2022-01-10', 9),
    (2, date '2022-01-15', 8),
    (3, date '2022-02-14', 9),
    (4, date '2022-02-18', 12),
    (5, date '2022-02-23', 15)
  ) a
)
select
  date_trunc('month', dt) as mon
  , sum(sum(val)) over(
    order by date_trunc('month', dt) asc
  ) as rsum
from t
group by 1
mon                    | rsum
:--------------------- | ---:
2022-01-01 00:00:00 00 |   17
2022-02-01 00:00:00 00 |   53

db<>fiddle enter image description here

mon rsum vers
2022-01-01 17 impalad version 3.4.0-SNAPSHOT RELEASE (build 27b919fc8a5907648349aa48eefc894e15a5a6d4) Built on Tue Aug 3 21:19:39 UTC 2021
2022-02-01 53 impalad version 3.4.0-SNAPSHOT RELEASE (build 27b919fc8a5907648349aa48eefc894e15a5a6d4) Built on Tue Aug 3 21:19:39 UTC 2021

CodePudding user response:

Maybe try this :

SELECT MONTH, SUM(value) FROM yourtable GROUP BY MONTH(month)

CodePudding user response:

Steps1: convert the date to month using date_trunc()

Step2: apply the aggregation function and grouup by

since impala has date_trunc() : https://impala.apache.org/docs/build/html/topics/impala_datetime_functions.html

  select
      date_trunc(date,'month') as month,
      sum(value) as total
    from [table name]
    group by 1
  • Related