我有一个表,其中包含许多字段,并且我尝试按除两个值外的所有值进行分组,这些值是我求和的。我想做
SELECT my_table.* except(value_1, value_2)
, sum(value_1)
, sum(value_2)
FROM my_table
GROUP BY my_table.* except(value_1, value_2)
但不幸的是GROUP BY my_table.* except(value_1, value_2)
不起作用。有什么建议吗?
答案 0 :(得分:3)
以下是用于BigQuery标准SQL
#standardSQL
SELECT DISTINCT * EXCEPT(value_1, value_2, grp),
SUM(value_1) OVER(PARTITION BY grp) sum_value_1,
SUM(value_2) OVER(PARTITION BY grp) sum_value_2
FROM (
SELECT *, REGEXP_REPLACE(TO_JSON_STRING(t), r'"(?:value_1|value_2)":.+?[,}]', '') grp
FROM `project.dataset.table` t
)
您可以使用下面的示例中的虚拟数据来测试,玩游戏
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 value_1, 2 value_2, 3 value_3, 4 value_4 UNION ALL
SELECT 11, 12, 3, 14 UNION ALL
SELECT 21, 22, 3, 14
)
SELECT DISTINCT * EXCEPT(value_1, value_2, grp),
SUM(value_1) OVER(PARTITION BY grp) sum_value_1,
SUM(value_2) OVER(PARTITION BY grp) sum_value_2
FROM (
SELECT *, REGEXP_REPLACE(TO_JSON_STRING(t), r'"(?:value_1|value_2)":.+?[,}]', '') grp
FROM `project.dataset.table` t
)
结果为
Row value_3 value_4 sum_value_1 sum_value_2
1 3 14 32 34
2 3 4 1 2
以上将处理任意数量的列,并且您不需要显式地引用所有列-仅那些要排除的列可以被显式引用-在此示例中为value_1和value_2