简体   繁体   English

BigQuery 中的 PIVOT 能否生成一个“总计”列,聚合所有维度行?

[英]Can PIVOT in BigQuery generate a "total" column, aggregated across all dimension rows?

Sample 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 |
+---------+-------+---------+------+

I can pivot it like this:我可以像这样 pivot:

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

and it gives me results:它给了我结果:

+---------+------+----------+----------+
| product | year | total_Q1 | total_Q2 |
+---------+------+----------+----------+
| Kale    | 2020 |       51 |       23 |
| Apple   | 2020 |       77 |        0 |
+---------+------+----------+----------+

Now I want to add column "total" that will contain all sales across product and year.现在我想添加“总计”列,其中包含跨产品和年份的所有销售额。 The result would be:结果将是:

+---------+------+----------+----------+-------+
| product | year | total_Q1 | total_Q2 | total |
+---------+------+----------+----------+-------+
| Kale    | 2020 |       51 |       23 |   122 |
| Apple   | 2020 |       77 |        0 |   77  |
+---------+------+----------+----------+-------+

How to do this?这该怎么做? Is it possible to do using PIVOT or I have to do separate GROUP BY SQL and join it with pivot results?是否可以使用 PIVOT 或我必须单独 GROUP BY SQL 并将其加入 pivot 结果?

Take a look at below query: You can calculate total over product and year using an analytic function first and then pivot a table to get the result you want.看看下面的查询:您可以先使用分析 function 然后使用表 pivot 计算产品和年份的total以获得您想要的结果。

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
;

output: output:

在此处输入图像描述

Consider below approach考虑以下方法

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    

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

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

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