简体   繁体   中英

Multiple Time series aggregation in SQL / Big query

I want to have a single table with multiple time series aggregations. For example, in this table,

user_id,date,conversions

1,11/08/2022,3

2,11/08/2022,1

Is it possible to have a single SQL query to generate a table like below:

user_id, conversions_last_7_days, conversions_last_30_days

We can use conditional aggregation here:

SELECT
    user_id,
    SUM(CASE WHEN "date" BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AND
               CURRENT_DATE() THEN conversions ELSE 0 END) AS conversions_last_7_days,
    SUM(CASE WHEN "date" BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY) AND
               CURRENT_DATE() THEN conversions ELSE 0 END) AS conversions_last_30_days
FROM yourTable
GROUP BY user_id;

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