简体   繁体   中英

Build time window counters from raw data - Big Query

Consider raw events data regarding purchases in 2020, as per the following table:

BUYER       DATE          ITEM 
Joe     '2020-01-15'      Dr. Pepper
Joe     '2020-02-15'      Dr. Pepper
Joe     '2020-03-15'      Dr. Pepper
Joe     '2020-05-15'      Dr. Pepper
Joe     '2020-10-15'      Dr. Pepper
Joe     '2020-12-15'      Dr. Pepper

I would like to aggregate the data to see what Joe did in a monthly moving sum, ie, obtaining as an outcome

BUYER  Date         Num_Purchases_last_3months
Joe   '2020-01-31'       1
Joe   '2020-02-31'       2
Joe   '2020-03-31'       3
Joe   '2020-04-31'       2
.
.
.
Joe   '2020-11-31'       1
Joe   '2020-12-31'       2

How could I obtain the desired result in an efficient query?

You can use window functions, in this case, count(*) with a range window frame specification:

select t.*,
       count(*) over (partition by buyer
                      order by extract(year from date) * 12 + extract(month from date)
                      range between 2 preceding and current row
                     ) as Num_Purchases_last_3months
from t;

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