简体   繁体   中英

How to aggregate YTD measure dynamically

I have a table which has 2 fields timestamp and count . Table has data since 2016 November.

I have to set up a query which will daily aggregate the YTD sum(count) for all the years. I am not using calendar year definition but rather November-October (Next year). This shouldn't ideally change the logic

2017: 11/01/2016-10/31/2017;
2018: 11/01/2017-10/31/2018;
2019: 11/01/2018-10/31/2019; 
2020: 11/01/2019-10/31/2020

I want a query that will calculate on any given day aggregate YTD with November 1st as the start date. I tried this query

select ytd_bucket
,sum(count_field) sum
from
(
select 
timestamp_field,
count_field,
CASE 
WHEN DATE(timestamp_field,"America/Los_Angeles") >= '2019-11-01' THEN '2020'
WHEN DATE(timestamp_field,"America/Los_Angeles") BETWEEN '2018-11-01' AND CAST(CONCAT('2019-',FORMAT_DATE('%m-%d', DATE(CURRENT_TIMESTAMP(),"America/Los_Angeles"))) AS DATE) THEN '2019'
WHEN DATE(timestamp_field,"America/Los_Angeles") BETWEEN '2017-11-01' AND CAST(CONCAT('2018-',FORMAT_DATE('%m-%d', DATE(CURRENT_TIMESTAMP(),"America/Los_Angeles"))) AS DATE) THEN '2018'
WHEN DATE(timestamp_field,"America/Los_Angeles") BETWEEN '2016-11-01' AND CAST(CONCAT('2017-',FORMAT_DATE('%m-%d', DATE(CURRENT_TIMESTAMP(),"America/Los_Angeles"))) AS DATE) THEN '2017'
ELSE NULL END as YTD_bucket
from table
)
group by 1

The above query does not aggregate the numbers are a YTD level. For the years prior to 2020 (ytd_bucket) the query is aggregating the entire years count.

Start by aggregating per day:

select date(timestamp_field, 'America/Los_Angeles') as dte,
       count(*)
from table
group by dte;

Then, for the YTD, you want to add one year and get the date:

select dte,
       count(*),
       sum(count(*)) over (partition by extract(year from date_add(dte, interval 1 month))
                           order by min(timestamp_field)
                          ) as running_cnt
from (select t.*,
             date(timestamp_field, 'America/Los_Angeles') as dte
      from t
     ) t
group by dte;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM