简体   繁体   English

SQL/Bigquery 中的多重聚合

[英]Multiple Aggregation in SQL/Bigquery

I have a table with called user_state with columns user_ID, account_id, Balance, Date.我有一个名为 user_state 的表,其中包含 user_ID、account_id、Balance、Date 列。 Each user_id can have multiple account.每个 user_id 可以有多个帐户。 Sample table looks like this: https://i.stack.imgur.com/w4odf.png you can create the table using script:示例表如下所示: https://i.stack.imgur.com/w4odf.png您可以使用脚本创建表:

    CREATE TABLE USER_STATE (USER_ID int, ACCOUNT_ID int, SNAPSHOT_DATE DATE, BALANCE float);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,1 ,'2016-07-01', 50);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,2 ,'2016-07-01', 50);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,2 ,'2016-07-05', 80);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,1, '2016-07-27', 150);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,1,'2016-07-31', 200);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,1, '2016-08-18', 150);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,2,'2016-08-21', 250);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(23,1,'2016-08-21', 250);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44,1, '2016-06-01', 10);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44,2, '2016-06-01', 20);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44, 1,'2016-06-05', 40);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44,1, '2016-06-27', 90);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44,1, '2016-06-31', 300);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44,1, '2016-09-18', 400);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44,1, '2016-09-21', 200);
    INSERT INTO USER_STATE(USER_ID,ACCOUNT_ID,SNAPSHOT_DATE,BALANCE) VALUES(44,2, '2016-09-21', 200);

I would like to get this values per month per user:我想每个用户每月获得这个值:

  • first_date of the month and the sum_balance for the first of the month per user.每月的 first_date 和每个用户当月第一天的 sum_balance。
  • last_date of the month and sum_balance for the last date of that month per user.每月的 last_date 和每个用户该月最后一天的 sum_balance。
  • ave_balance for the each month per user.每个用户每个月的 ave_balance。

Final Result should look like this: https://i.stack.imgur.com/Fyjes.png最终结果应如下所示: https://i.stack.imgur.com/Fyjes.png

One solution is to get the item I listed and I need per month per user separately and join the tables, Is there more efficient and easier solution for it?一种解决方案是分别获取我列出的项目和每个用户每月需要的项目并加入表格,是否有更有效和更简单的解决方案?

Thanks, Bikram谢谢,比克拉姆

Hmmm.嗯。 . . . . aggregate by snapshot date and user and then by user and month:按快照日期和用户聚合,然后按用户和月份聚合:

select user_id, date_trunc(snapshot_date, month) as yyyymm,
       min(snapshot_date) as first_date,
       array_agg(balance order by snapshot_date limit 1)[ordinal(1)] as first_balance,
       max(snapshot_date) as first_date,
       array_agg(balance order by snapshot_date desc limit 1)[ordinal(1)] as last_balance,
       avg(balance)
from (select user_id, snapshot_date, sum(balance) as balance
      from user_state u
      group by 1, 2
     ) u
group by 1, 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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