提问人:164_user 提问时间:3/30/2023 更新时间:4/1/2023 访问量:182
GROUP BY 是否发生在 SELECT 语句中发生的操作之后?(大查询)
Does the GROUP BY occur after the operations that occur in the SELECT statement? (BigQuery)
问:
从我所读到的,例如 我知道SQL语句将按以下顺序执行:https://www.sisense.com/blog/sql-query-order-of-operations/
1:发件人,2:其中,3:分组依据,4:HAVINIG,5:选择,6:订购依据,7:限制
这对我来说是有道理的,除了您可以对在 SELECT 语句中创建的新列进行 GROUP BY 这一事实。例如:
WITH data_sample AS (
SELECT "m1" as meter, 2 as value UNION ALL
SELECT "m1" as meter, 3 as value, UNION ALL
SELECT "m1" as meter, 5 as value, UNION ALL
SELECT "m1" as meter, 5 as value UNION ALL
SELECT "m2" as meter, 4 as value UNION ALL
SELECT "m2" as meter, 6 as value UNION ALL
SELECT "m3" as meter, 4 as value UNION ALL
SELECT "m3" as meter, 4 as value )
SELECT IF(meter="m1", meter, "m7") as meter_new, SUM(value)
FROM data_sample
GROUP BY meter_new
在这里,在 SELECT 语句中创建的新列已经可供 GROUP BY 使用(这让我认为 SELECT 在 GROUP BY 之前执行),但此分组随后用于 SUM 语句,该语句位于查询的 SELECT 部分,使 SELECT 似乎也发生在 GROUP BY 之后。meter_new
显然我在这里遗漏了一些东西,但谷歌搜索只是不断返回相同的有序操作列表,这对我没有帮助。
答:
评论
meter_new
IF(meter="m1", meter, "m7")