为什么 SUM() 在 IF 语句中的位置在 GROUP BY 场景 (BigQuery) 中很重要?

问题描述 投票:0回答:0

我有一些电表数据,我想按电表分组并将所有数据转换为基线单位。下面的查询是这样做的:

SELECT meter, 
   IF(unit="kWh" OR unit="kW", "kW", IF(unit="kVAr" OR unit="kVArh", "kVAr", NULL)) as unit,
   SUM(IF(unit="kWh" OR unit="kVArh", value*2, value)) as value,     

FROM data_sample
GROUP BY meter, unit

但是,如果我将第二行更改为

IF(unit="kWh" OR unit="kVArh", SUM(value*2), SUM(value)) as value
,换句话说,如果我将 SUM() 移动到 IF 语句中,我会收到错误消息:“SELECT 列表表达式引用既未分组也未聚合的列单元

奇怪的是,如果我删除单元的 IF 语句,例如

SELECT 
   meter, unit,
   SUM(IF(unit="kWh" OR unit="kVArh", value*2, value)) as value,
   IF(unit="kWh" OR unit="kVArh", SUM(value*2), SUM(value)) as value2,
   
FROM data_sample
GROUP BY meter, unit

然后它起作用了,对 value 和 value2 给出了相同的结果。我不明白为什么会这样,我认为这与处理顺序有关?如果有人可以帮助我理解这一点。

示例数据:

WITH data_sample AS (
   SELECT "m1" as meter, "kWh" as unit, 2 as value UNION ALL 
   SELECT "m1" as meter, "kWh" as unit, 3 as value, UNION ALL 
   SELECT "m1" as meter, "kVArh" as unit, 5 as value, UNION ALL
   SELECT "m1" as meter, "kW" as unit, 5 as value UNION ALL
   SELECT "m2" as meter, "kWh" as unit, 4 as value UNION ALL
   SELECT "m2" as meter, "kW" as unit, 6 as value UNION ALL 
   SELECT "m3" as meter, "kW" as unit, 4 as value UNION ALL
   SELECT "m3" as meter, "kVAr" as unit, 4 as value )

示例数据(由上面的代码生成)

期望的输出(由第一个代码片段生成):

(即,如果单位是 kWh 或 kVarh,则通过乘以 2 将值分别转换为 kW 和 kVar,并在对每个电表的所有 kW 和 kVAr 值求和之前相应地重命名单位)

sql group-by google-bigquery sql-execution-plan order-of-execution
© www.soinside.com 2019 - 2024. All rights reserved.