BigQuery 中的 PIVOT 能否生成一个 "total" 列,聚合所有维度行?
Can PIVOT in BigQuery generate a "total" column, aggregated across all dimension rows?
样本table:
+---------+-------+---------+------+
| product | sales | quarter | year |
+---------+-------+---------+------|
| Kale | 51 | Q1 | 2020 |
| Kale | 23 | Q2 | 2020 |
| Kale | 45 | Q3 | 2020 |
| Kale | 3 | Q4 | 2020 |
| Kale | 70 | Q1 | 2021 |
| Kale | 85 | Q2 | 2021 |
| Apple | 77 | Q1 | 2020 |
| Apple | 0 | Q2 | 2020 |
| Apple | 1 | Q1 | 2021 |
+---------+-------+---------+------+
我可以这样转动它:
WITH t AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter, 2020 as year UNION ALL
SELECT 'Kale', 23, 'Q2', 2020 UNION ALL
SELECT 'Kale', 45, 'Q3', 2020 UNION ALL
SELECT 'Kale', 3, 'Q4', 2020 UNION ALL
SELECT 'Kale', 70, 'Q1', 2021 UNION ALL
SELECT 'Kale', 85, 'Q2', 2021 UNION ALL
SELECT 'Apple', 77, 'Q1', 2020 UNION ALL
SELECT 'Apple', 0, 'Q2', 2020 UNION ALL
SELECT 'Apple', 1, 'Q1', 2021
)
SELECT *
FROM t
PIVOT (
sum(sales) as total
FOR quarter in ('Q1', 'Q2')
)
WHERE year = 2020
它给了我结果:
+---------+------+----------+----------+
| product | year | total_Q1 | total_Q2 |
+---------+------+----------+----------+
| Kale | 2020 | 51 | 23 |
| Apple | 2020 | 77 | 0 |
+---------+------+----------+----------+
现在我想添加“总计”列,其中将包含跨产品和年份的所有销售额。结果将是:
+---------+------+----------+----------+-------+
| product | year | total_Q1 | total_Q2 | total |
+---------+------+----------+----------+-------+
| Kale | 2020 | 51 | 23 | 122 |
| Apple | 2020 | 77 | 0 | 77 |
+---------+------+----------+----------+-------+
如何做到这一点?是否可以使用 PIVOT 或我必须单独进行 GROUP BY SQL 并将其与数据透视结果结合起来?
看看下面的查询:您可以先使用分析函数计算 total
产品和年份,然后旋转 table 以获得您想要的结果。
WITH t AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter, 2020 as year UNION ALL
SELECT 'Kale', 23, 'Q2', 2020 UNION ALL
SELECT 'Kale', 45, 'Q3', 2020 UNION ALL
SELECT 'Kale', 3, 'Q4', 2020 UNION ALL
SELECT 'Kale', 70, 'Q1', 2021 UNION ALL
SELECT 'Kale', 85, 'Q2', 2021 UNION ALL
SELECT 'Apple', 77, 'Q1', 2020 UNION ALL
SELECT 'Apple', 0, 'Q2', 2020 UNION ALL
SELECT 'Apple', 1, 'Q1', 2021
)
SELECT * FROM (
SELECT *, SUM(sales) OVER (PARTITION BY product, year) AS total FROM t
) PIVOT (SUM(sales) total FOR quarter IN ('Q1', 'Q2'))
WHERE year = 2020
;
输出:
考虑以下方法
select * from (
select * from your_table union all
select product, sum(sales), 'Year', year
from your_table
group by product, year
)
pivot (sum(sales) as total for quarter in ('Year', 'Q1', 'Q2'))
where year = 2020
如果应用于您问题中的示例数据 - 输出为
样本table:
+---------+-------+---------+------+
| product | sales | quarter | year |
+---------+-------+---------+------|
| Kale | 51 | Q1 | 2020 |
| Kale | 23 | Q2 | 2020 |
| Kale | 45 | Q3 | 2020 |
| Kale | 3 | Q4 | 2020 |
| Kale | 70 | Q1 | 2021 |
| Kale | 85 | Q2 | 2021 |
| Apple | 77 | Q1 | 2020 |
| Apple | 0 | Q2 | 2020 |
| Apple | 1 | Q1 | 2021 |
+---------+-------+---------+------+
我可以这样转动它:
WITH t AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter, 2020 as year UNION ALL
SELECT 'Kale', 23, 'Q2', 2020 UNION ALL
SELECT 'Kale', 45, 'Q3', 2020 UNION ALL
SELECT 'Kale', 3, 'Q4', 2020 UNION ALL
SELECT 'Kale', 70, 'Q1', 2021 UNION ALL
SELECT 'Kale', 85, 'Q2', 2021 UNION ALL
SELECT 'Apple', 77, 'Q1', 2020 UNION ALL
SELECT 'Apple', 0, 'Q2', 2020 UNION ALL
SELECT 'Apple', 1, 'Q1', 2021
)
SELECT *
FROM t
PIVOT (
sum(sales) as total
FOR quarter in ('Q1', 'Q2')
)
WHERE year = 2020
它给了我结果:
+---------+------+----------+----------+
| product | year | total_Q1 | total_Q2 |
+---------+------+----------+----------+
| Kale | 2020 | 51 | 23 |
| Apple | 2020 | 77 | 0 |
+---------+------+----------+----------+
现在我想添加“总计”列,其中将包含跨产品和年份的所有销售额。结果将是:
+---------+------+----------+----------+-------+
| product | year | total_Q1 | total_Q2 | total |
+---------+------+----------+----------+-------+
| Kale | 2020 | 51 | 23 | 122 |
| Apple | 2020 | 77 | 0 | 77 |
+---------+------+----------+----------+-------+
如何做到这一点?是否可以使用 PIVOT 或我必须单独进行 GROUP BY SQL 并将其与数据透视结果结合起来?
看看下面的查询:您可以先使用分析函数计算 total
产品和年份,然后旋转 table 以获得您想要的结果。
WITH t AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter, 2020 as year UNION ALL
SELECT 'Kale', 23, 'Q2', 2020 UNION ALL
SELECT 'Kale', 45, 'Q3', 2020 UNION ALL
SELECT 'Kale', 3, 'Q4', 2020 UNION ALL
SELECT 'Kale', 70, 'Q1', 2021 UNION ALL
SELECT 'Kale', 85, 'Q2', 2021 UNION ALL
SELECT 'Apple', 77, 'Q1', 2020 UNION ALL
SELECT 'Apple', 0, 'Q2', 2020 UNION ALL
SELECT 'Apple', 1, 'Q1', 2021
)
SELECT * FROM (
SELECT *, SUM(sales) OVER (PARTITION BY product, year) AS total FROM t
) PIVOT (SUM(sales) total FOR quarter IN ('Q1', 'Q2'))
WHERE year = 2020
;
输出:
考虑以下方法
select * from (
select * from your_table union all
select product, sum(sales), 'Year', year
from your_table
group by product, year
)
pivot (sum(sales) as total for quarter in ('Year', 'Q1', 'Q2'))
where year = 2020
如果应用于您问题中的示例数据 - 输出为