I am working in Bigquery and I need the percentages for each result for each month, I have the following query but the percentage is calculated with respect to the total, I have tried to add a PARTITION BY in the OVER clause but it does not work.
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 |
