使用 GROUP BY ROLLUP 透视 table 数据
Pivot table data using GROUP BY ROLLUP
[注意:这里可以使用任何支持ROLLUP的SQL数据库。我在示例中使用了 BigQuery。]
我希望获取 Excel 中以下数据透视表的所有数据:
应该有 36 个数据单元格。到目前为止,我的 BigQuery 查询如下:
SELECT Year, Quarter, ProductGroup, Product, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ROLLUP(ProductGroup, Product, Year, Quarter)
union distinct
SELECT Year, Quarter, ProductGroup, Product, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ROLLUP(Year, Quarter, Product, ProductGroup)
但是,它只给了我 34 个结果,而我正在寻找 36 个结果。在此查询中提供所有 pivot-table 组合的正确方法是什么?此处附上数据:
$ cat > Products.csv
ProductGroup,Product,Year,Quarter,Revenue,Units,Count,Product Key,Reseller,Product Info,QuarterAsNumber
Electronics,Phone,2018,Q1,103,7,1,2018-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2018,Q1,102,4,1,2018-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2019,Q1,98,12,1,2019-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Computer,2018,Q1,104,3,1,2018-Q1,Samsung,Format=XML; <Properties>…,1
Electronics,Computer,2019,Q1,83,7,1,2019-Q1,Google,Format=XML; <Properties>…,1
Media,Theater,2018,Q1,17,4,1,2018-Q1,Sony,Format=XML; <Properties>…,1
Media,Theater,2019,Q1,20,7,1,2019-Q1,Sony,Format=XML; <Properties>…,1
Media,Movies,2018,Q1,25,12,1,2018-Q1,Microsoft,Format=XML; <Properties>…,1
Media,Movies,2019,Q1,26,13,1,2019-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2018,Q2,105,5,1,2018-Q2,Samsung,Format=XML; <Properties>…,2
Electronics,Phone,2019,Q2,82,15,1,2019-Q2,LG,Format=XML; <Properties>…,2
Electronics,Computer,2018,Q2,99,4,1,2018-Q2,LG,Format=XML; <Properties>…,2
Electronics,Computer,2019,Q2,84,20,1,2019-Q2,Sony,Format=XML; <Properties>…,2
Media,Theater,2018,Q2,17,4,1,2018-Q2,Microsoft,Format=XML; <Properties>…,2
Media,Theater,2019,Q2,22,5,1,2019-Q2,Sony,Format=XML; <Properties>…,2
Media,Movies,2018,Q2,25,12,1,2018-Q2,Samsung,Format=XML; <Properties>…,2
Media,Movies,2019,Q2,26,14,1,2019-Q2,Google,Format=XML; <Properties>…,2
Electronics,Phone,2000,Q1,103,7,1,2000-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2001,Q1,102,4,1,2001-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2002,Q1,98,12,1,2002-Q1,Microsoft,Format=XML; <Properties>…,1
Electronics,Computer,2003,Q1,104,3,1,2003-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Computer,2004,Q1,83,7,1,2004-Q1,Samsung,Format=XML; <Properties>…,1
Media,Theater,2005,Q1,17,4,1,2005-Q1,Google,Format=XML; <Properties>…,1
Media,Theater,2006,Q1,20,7,1,2006-Q1,Sony,Format=XML; <Properties>…,1
Media,Movies,2007,Q1,25,12,1,2007-Q1,Sony,Format=XML; <Properties>…,1
Media,Movies,2008,Q1,26,13,1,2008-Q1,Microsoft,Format=XML; <Properties>…,1
Electronics,Phone,2009,Q2,105,5,1,2009-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Phone,2010,Q2,82,15,1,2010-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Computer,2011,Q2,99,4,1,2011-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Computer,2012,Q2,84,20,1,2012-Q2,Sony,Format=XML; <Properties>…,2
Media,Theater,2013,Q2,17,4,1,2013-Q2,Sony,Format=XML; <Properties>…,2
Media,Theater,2014,Q2,22,5,1,2014-Q2,Sony,Format=XML; <Properties>…,2
Media,Movies,2015,Q2,25,12,1,2015-Q2,Sony,Format=XML; <Properties>…,2
Media,Movies,2016,Q2,26,14,1,2016-Q2,Samsung,Format=XML; <Properties>…,2
Media,Movies,2017,Q1,26,13,1,2017-Q1,Google,Format=XML; <Properties>…,1
Electronics,Phone,2018,Q2,105,5,1,2018-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Phone,2019,Q2,82,15,1,2019-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Computer,2020,Q2,99,4,1,2020-Q2,Microsoft,Format=XML; <Properties>…,2
Electronics,Phone,2020,Q1,103,7,1,2020-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2020,Q2,102,4,1,2020-Q2,Samsung,Format=XML; <Properties>…,2
Electronics,Phone,2020,Q3,98,12,1,2020-Q3,LG,Format=XML; <Properties>…,3
Electronics,Computer,2020,Q4,104,3,1,2020-Q4,LG,Format=XML; <Properties>…,4
Electronics,Computer,2020,Q1,83,7,1,2020-Q1,Sony,Format=XML; <Properties>…,1
Media,Theater,2020,Q1,17,4,1,2020-Q1,Microsoft,Format=XML; <Properties>…,1
Media,Theater,2020,Q1,20,7,1,2020-Q1,Sony,Format=XML; <Properties>…,1
这是我正在寻找的长格式:
-- >, ProductGroup>
SELECT ProductGroup, NULL, NULL, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ROLLUP (ProductGroup)
-- ProductGroup>Product
union distinct
SELECT ProductGroup, Product, NULL, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Product
-- ProductGroup>Product>Year
union distinct
SELECT ProductGroup, Product, Year, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Product, Year
-- ProductGroup>Year
union distinct
SELECT ProductGroup, NULL, Year, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Year
-- ProductGroup>Year>Quarter
union distinct
SELECT ProductGroup, NULL, Year, Quarter, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Year, Quarter
-- ProductGroup>Product>Year>Quarter
union distinct
SELECT ProductGroup , Product, Year, Quarter, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Product, Year, Quarter
-- Year>Quarter
union distinct
SELECT NULL , NULL, Year, Quarter, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY Year, Quarter
-- Year
union distinct
SELECT NULL , NULL, Year, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY Year
BigQuery 没有 Cube 或 Grouping Set,因此您可以使用以下技巧 - 对我来说看起来很通用,可以扩展到任何维度,同时避免冗余代码行
select
(case when grp_set & 1 > 0 then ProductGroup end) as ProductGroup,
(case when grp_set & 2 > 0 then Product end) as Product,
(case when grp_set & 4 > 0 then Year end) as Year,
(case when grp_set & 8 > 0 then Quarter end) as Quarter,
sum(Revenue) as Revenue,
sum(Units) as Units
from `first-outlet-750.biengine_tutorial.Product`, unnest(generate_array(1, 16)) grp_set
where Year IN (2020) and Quarter in ('Q1')
group by 1, 2, 3, 4
having not (Year is null and not Quarter is null)
and not (ProductGroup is null and not Product is null)
-- order by 1, 2, 3, 4
如果应用于您问题中的示例数据 - 输出为
[注意:这里可以使用任何支持ROLLUP的SQL数据库。我在示例中使用了 BigQuery。]
我希望获取 Excel 中以下数据透视表的所有数据:
应该有 36 个数据单元格。到目前为止,我的 BigQuery 查询如下:
SELECT Year, Quarter, ProductGroup, Product, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ROLLUP(ProductGroup, Product, Year, Quarter)
union distinct
SELECT Year, Quarter, ProductGroup, Product, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ROLLUP(Year, Quarter, Product, ProductGroup)
但是,它只给了我 34 个结果,而我正在寻找 36 个结果。在此查询中提供所有 pivot-table 组合的正确方法是什么?此处附上数据:
$ cat > Products.csv
ProductGroup,Product,Year,Quarter,Revenue,Units,Count,Product Key,Reseller,Product Info,QuarterAsNumber
Electronics,Phone,2018,Q1,103,7,1,2018-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2018,Q1,102,4,1,2018-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2019,Q1,98,12,1,2019-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Computer,2018,Q1,104,3,1,2018-Q1,Samsung,Format=XML; <Properties>…,1
Electronics,Computer,2019,Q1,83,7,1,2019-Q1,Google,Format=XML; <Properties>…,1
Media,Theater,2018,Q1,17,4,1,2018-Q1,Sony,Format=XML; <Properties>…,1
Media,Theater,2019,Q1,20,7,1,2019-Q1,Sony,Format=XML; <Properties>…,1
Media,Movies,2018,Q1,25,12,1,2018-Q1,Microsoft,Format=XML; <Properties>…,1
Media,Movies,2019,Q1,26,13,1,2019-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2018,Q2,105,5,1,2018-Q2,Samsung,Format=XML; <Properties>…,2
Electronics,Phone,2019,Q2,82,15,1,2019-Q2,LG,Format=XML; <Properties>…,2
Electronics,Computer,2018,Q2,99,4,1,2018-Q2,LG,Format=XML; <Properties>…,2
Electronics,Computer,2019,Q2,84,20,1,2019-Q2,Sony,Format=XML; <Properties>…,2
Media,Theater,2018,Q2,17,4,1,2018-Q2,Microsoft,Format=XML; <Properties>…,2
Media,Theater,2019,Q2,22,5,1,2019-Q2,Sony,Format=XML; <Properties>…,2
Media,Movies,2018,Q2,25,12,1,2018-Q2,Samsung,Format=XML; <Properties>…,2
Media,Movies,2019,Q2,26,14,1,2019-Q2,Google,Format=XML; <Properties>…,2
Electronics,Phone,2000,Q1,103,7,1,2000-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2001,Q1,102,4,1,2001-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2002,Q1,98,12,1,2002-Q1,Microsoft,Format=XML; <Properties>…,1
Electronics,Computer,2003,Q1,104,3,1,2003-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Computer,2004,Q1,83,7,1,2004-Q1,Samsung,Format=XML; <Properties>…,1
Media,Theater,2005,Q1,17,4,1,2005-Q1,Google,Format=XML; <Properties>…,1
Media,Theater,2006,Q1,20,7,1,2006-Q1,Sony,Format=XML; <Properties>…,1
Media,Movies,2007,Q1,25,12,1,2007-Q1,Sony,Format=XML; <Properties>…,1
Media,Movies,2008,Q1,26,13,1,2008-Q1,Microsoft,Format=XML; <Properties>…,1
Electronics,Phone,2009,Q2,105,5,1,2009-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Phone,2010,Q2,82,15,1,2010-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Computer,2011,Q2,99,4,1,2011-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Computer,2012,Q2,84,20,1,2012-Q2,Sony,Format=XML; <Properties>…,2
Media,Theater,2013,Q2,17,4,1,2013-Q2,Sony,Format=XML; <Properties>…,2
Media,Theater,2014,Q2,22,5,1,2014-Q2,Sony,Format=XML; <Properties>…,2
Media,Movies,2015,Q2,25,12,1,2015-Q2,Sony,Format=XML; <Properties>…,2
Media,Movies,2016,Q2,26,14,1,2016-Q2,Samsung,Format=XML; <Properties>…,2
Media,Movies,2017,Q1,26,13,1,2017-Q1,Google,Format=XML; <Properties>…,1
Electronics,Phone,2018,Q2,105,5,1,2018-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Phone,2019,Q2,82,15,1,2019-Q2,Sony,Format=XML; <Properties>…,2
Electronics,Computer,2020,Q2,99,4,1,2020-Q2,Microsoft,Format=XML; <Properties>…,2
Electronics,Phone,2020,Q1,103,7,1,2020-Q1,Sony,Format=XML; <Properties>…,1
Electronics,Phone,2020,Q2,102,4,1,2020-Q2,Samsung,Format=XML; <Properties>…,2
Electronics,Phone,2020,Q3,98,12,1,2020-Q3,LG,Format=XML; <Properties>…,3
Electronics,Computer,2020,Q4,104,3,1,2020-Q4,LG,Format=XML; <Properties>…,4
Electronics,Computer,2020,Q1,83,7,1,2020-Q1,Sony,Format=XML; <Properties>…,1
Media,Theater,2020,Q1,17,4,1,2020-Q1,Microsoft,Format=XML; <Properties>…,1
Media,Theater,2020,Q1,20,7,1,2020-Q1,Sony,Format=XML; <Properties>…,1
这是我正在寻找的长格式:
-- >, ProductGroup>
SELECT ProductGroup, NULL, NULL, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ROLLUP (ProductGroup)
-- ProductGroup>Product
union distinct
SELECT ProductGroup, Product, NULL, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Product
-- ProductGroup>Product>Year
union distinct
SELECT ProductGroup, Product, Year, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Product, Year
-- ProductGroup>Year
union distinct
SELECT ProductGroup, NULL, Year, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Year
-- ProductGroup>Year>Quarter
union distinct
SELECT ProductGroup, NULL, Year, Quarter, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Year, Quarter
-- ProductGroup>Product>Year>Quarter
union distinct
SELECT ProductGroup , Product, Year, Quarter, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY ProductGroup, Product, Year, Quarter
-- Year>Quarter
union distinct
SELECT NULL , NULL, Year, Quarter, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY Year, Quarter
-- Year
union distinct
SELECT NULL , NULL, Year, NULL, SUM(Revenue) AS SumOfRevenue, SUM(Units) as SumOfUnits FROM `first-outlet-750.biengine_tutorial.Product`
WHERE Year IN (2020) and Quarter in ('Q1')
GROUP BY Year
BigQuery 没有 Cube 或 Grouping Set,因此您可以使用以下技巧 - 对我来说看起来很通用,可以扩展到任何维度,同时避免冗余代码行
select
(case when grp_set & 1 > 0 then ProductGroup end) as ProductGroup,
(case when grp_set & 2 > 0 then Product end) as Product,
(case when grp_set & 4 > 0 then Year end) as Year,
(case when grp_set & 8 > 0 then Quarter end) as Quarter,
sum(Revenue) as Revenue,
sum(Units) as Units
from `first-outlet-750.biengine_tutorial.Product`, unnest(generate_array(1, 16)) grp_set
where Year IN (2020) and Quarter in ('Q1')
group by 1, 2, 3, 4
having not (Year is null and not Quarter is null)
and not (ProductGroup is null and not Product is null)
-- order by 1, 2, 3, 4
如果应用于您问题中的示例数据 - 输出为