每月百分比

percentage per month Bigquery

我在 Bigquery 工作,我需要每个月每个结果的百分比,我有以下查询,但百分比是相对于总数计算的,我试图在 OVER 子句中添加 PARTITION BY但它不起作用。

SELECT CAST(TIMESTAMP_TRUNC(CAST((created_at) AS TIMESTAMP), MONTH) AS DATE) AS `month`,
  result,
     count(*) * 100.0 / sum(count(1)) over() as percentage
FROM table_name

GROUP BY  1,2
ORDER BY  1
month result percentage
2021-01 0001 50
2021-01 0000 50
2021-02 00001 33.33
2021-02 0000 33.33
2021-02 0002 33.33

此示例是 dbFiddle SQL 服务器上的代码,但根据文档 google-bigquery 具有函数 COUNT( ~ ) OVER ( PARTITION BY ~ )

create table table_name(month char(7), result int)
insert into table_name values
('2021-01',50),
('2021-01',30),
('2021-01',20),
('2021-02',70),
('2021-02',80);
select 
  month, 
  result, 
  sum(result) over (partition by month) month_total, 
  100 * result / sum(result) over (partition by month) per_cent
from table_name
order by month, result;
month   | result | month_total | per_cent
:------ | -----: | ----------: | -------:
2021-01 |     20 |         100 |       20
2021-01 |     30 |         100 |       30
2021-01 |     50 |         100 |       50
2021-02 |     70 |         150 |       46
2021-02 |     80 |         150 |       53

db<>fiddle here

使用您分享的数据:

WITH data as(
SELECT "2021-01-01" as created_at,"0001" as result UNION ALL
SELECT "2021-01-01","0000" UNION ALL
SELECT "2021-02-01","00001"UNION ALL
SELECT "2021-02-01","0000"UNION ALL
SELECT "2021-02-01","0002"
)

我用子查询帮你处理了月份字段,然后用那个字段分区,然后按月份分组,结果。

d as (SELECT  CAST(TIMESTAMP_TRUNC(CAST((created_at) AS TIMESTAMP), MONTH) AS DATE) AS month,
 result, created_at
 from DATA
 )
SELECT d.month,
 d.result,
    count(*) * 100.0 / sum(count(1)) over(partition by month) as percentage
FROM d
 
GROUP BY  1, 2
ORDER BY  1

输出如下: