Snowflake: "SQL compilation error:... is not a valid group by expression"
Snowflake: "SQL compilation error:... is not a valid group by expression"
在不求助于 CTE 或子查询的情况下,有没有办法使用 Window 具有与 GROUP BY 不同的汇总级别的功能? COUNT(*) 有效,但如果在 COUNT 中指定了列名或使用了 SUM 函数,则查询错误 "is not a valid group by expression"。即使 PARTITION BY 列与 GROUP BY 相同,也会出现错误结果。
注释掉的行会导致查询失败。 正是针对这些类型的事情,人们首先想要使用 Window 功能。
create table sales (product_id integer, retail_price real, quantity integer, city varchar, state varchar);
insert into sales (product_id, retail_price, quantity, city, state) values
(1, 2.00, 1, 'SF', 'CA'),
(1, 2.00, 2, 'SJ', 'CA'),
(2, 5.00, 4, 'SF', 'CA'),
(2, 5.00, 8, 'SJ', 'CA'),
(2, 5.00, 16, 'Miami', 'FL'),
(2, 5.00, 32, 'Orlando', 'FL'),
(2, 5.00, 64, 'SJ', 'PR');
select city, state
, count(*) as city_sale_cnt
, count(*) over ( partition by state) as state_sale_cnt
-- , count(product_id) over ( partition by state) as state_sale_cnt2
, sum(retail_price) as city_price
-- , sum(retail_price) over ( partition by state) as state_price
from sales
group by 1,2;
docs 指示 Window 功能可能会导致问题,包括模糊警告 "PARTITION BY is not always compatible with GROUP BY.":
错误消息 SQL compilation error: ... is not a valid group by expression 通常表示 SELECT 语句的“项目”子句中的不同列未按相同方式分区,因此可能会产生不同的行数。
注释掉的代码不正确。原因是window函数解析了"after"group by
,group by
后没有product_id
或retail_price
。
这很容易解决:
select city, state,
count(*) as city_sale_cnt,
count(*) over (partition by state) as state_sale_cnt,
sum(count(product_id)) over (partition by state) as ,
sum(retail_price) as city_price,
sum(sum(retail_price)) over ( partition by state) as state_price
from sales
group by 1, 2;
起初,在聚合查询中使用 window 函数看起来有点混乱——嵌套的聚合函数看起来很别扭。我发现,虽然使用语法很容易,但是一旦你使用了几次。
尽管 snowflake 可能允许这样做(正如 Gordon Linoff 所证明的那样),但我提倡包装聚合查询并在外部查询中使用 window 函数。
很少有 RDBMS 允许混合使用 window 函数和聚合,生成的查询通常很难理解(除非你是像 Gordon 这样的真正的 SQL 向导!)。
select
t.*,
sum(city_sale_cnt) over (partition by state) as state_sale_cnt,
sum(city_price) over ( partition by state) as state_price
from (
select
city,
state,
count(*) as city_sale_cnt,
sum(retail_price) as city_price
from sales
group by 1,2
) t;
在不求助于 CTE 或子查询的情况下,有没有办法使用 Window 具有与 GROUP BY 不同的汇总级别的功能? COUNT(*) 有效,但如果在 COUNT 中指定了列名或使用了 SUM 函数,则查询错误 "is not a valid group by expression"。即使 PARTITION BY 列与 GROUP BY 相同,也会出现错误结果。
注释掉的行会导致查询失败。 正是针对这些类型的事情,人们首先想要使用 Window 功能。
create table sales (product_id integer, retail_price real, quantity integer, city varchar, state varchar);
insert into sales (product_id, retail_price, quantity, city, state) values
(1, 2.00, 1, 'SF', 'CA'),
(1, 2.00, 2, 'SJ', 'CA'),
(2, 5.00, 4, 'SF', 'CA'),
(2, 5.00, 8, 'SJ', 'CA'),
(2, 5.00, 16, 'Miami', 'FL'),
(2, 5.00, 32, 'Orlando', 'FL'),
(2, 5.00, 64, 'SJ', 'PR');
select city, state
, count(*) as city_sale_cnt
, count(*) over ( partition by state) as state_sale_cnt
-- , count(product_id) over ( partition by state) as state_sale_cnt2
, sum(retail_price) as city_price
-- , sum(retail_price) over ( partition by state) as state_price
from sales
group by 1,2;
docs 指示 Window 功能可能会导致问题,包括模糊警告 "PARTITION BY is not always compatible with GROUP BY.": 错误消息 SQL compilation error: ... is not a valid group by expression 通常表示 SELECT 语句的“项目”子句中的不同列未按相同方式分区,因此可能会产生不同的行数。
注释掉的代码不正确。原因是window函数解析了"after"group by
,group by
后没有product_id
或retail_price
。
这很容易解决:
select city, state,
count(*) as city_sale_cnt,
count(*) over (partition by state) as state_sale_cnt,
sum(count(product_id)) over (partition by state) as ,
sum(retail_price) as city_price,
sum(sum(retail_price)) over ( partition by state) as state_price
from sales
group by 1, 2;
起初,在聚合查询中使用 window 函数看起来有点混乱——嵌套的聚合函数看起来很别扭。我发现,虽然使用语法很容易,但是一旦你使用了几次。
尽管 snowflake 可能允许这样做(正如 Gordon Linoff 所证明的那样),但我提倡包装聚合查询并在外部查询中使用 window 函数。
很少有 RDBMS 允许混合使用 window 函数和聚合,生成的查询通常很难理解(除非你是像 Gordon 这样的真正的 SQL 向导!)。
select
t.*,
sum(city_sale_cnt) over (partition by state) as state_sale_cnt,
sum(city_price) over ( partition by state) as state_price
from (
select
city,
state,
count(*) as city_sale_cnt,
sum(retail_price) as city_price
from sales
group by 1,2
) t;