SNOWFLAKE 函数 SUM 不支持累积窗框
Posted
技术标签:
【中文标题】SNOWFLAKE 函数 SUM 不支持累积窗框【英文标题】:SNOWFLAKE Cumulative window frame unsupported for function SUM 【发布时间】:2021-10-26 13:16:57 【问题描述】:我正在使用 SNOWFLAKE snowflake 数据库。
我正在尝试使用雪花SNOWFLAKE_SAMPLE_DATA
数据库的一些Windows操作员,我遇到了累积函数的情况。
不工作的 SQL 如下:
SELECT
SUM( C_BIRTH_DAY )
OVER (PARTITION BY
C_BIRTH_MONTH,
C_BIRTH_YEAR
ORDER BY
NVL(C_BIRTH_COUNTRY,'COSTA RICA')
RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS MAX1,
MIN( C_BIRTH_DAY )
OVER ( PARTITION BY C_BIRTH_MONTH ,
C_BIRTH_YEAR
ORDER BY
NVL(C_BIRTH_COUNTRY,'COSTA RICA')
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS MIN1
FROM CUSTOMER;
提供的 SQL 将失败并返回错误消息:
SNOWFLAKE Cumulative window frame unsupported for function SUM
.
并且预期的结果是应该执行查询。
我用几个运算符 AVG
,MAX
,SUM
尝试了这个查询,结果都一样。
查看documentation 看起来语法很好:
对于累积窗口
cumulativeFrame ::=
ROWS | RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
| ROWS | RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
对于滑动窗口:
slidingFrame ::=
ROWS BETWEEN <N> PRECEDING | FOLLOWING AND <N> PRECEDING | FOLLOWING
| ROWS BETWEEN UNBOUNDED PRECEDING AND <N> PRECEDING | FOLLOWING
| ROWS BETWEEN <N> PRECEDING | FOLLOWING AND UNBOUNDED FOLLOWING
所以我目前找不到这个错误的解释。
【问题讨论】:
【参考方案1】:在尝试了各种各样的事情后,我发现如果我切换窗口语法而不是使用 RANGE,我会使用这样的行:
SELECT
SUM( C_BIRTH_DAY )
OVER (PARTITION BY
C_BIRTH_MONTH,
C_BIRTH_YEAR
ORDER BY
NVL(C_BIRTH_COUNTRY,'CR')
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS MAX1,
MIN( C_BIRTH_DAY )
OVER ( PARTITION BY C_BIRTH_MONTH ,
C_BIRTH_YEAR
ORDER BY
NVL(C_BIRTH_COUNTRY,'CR')
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS MIN1
FROM CUSTOMER;
有效!!
【讨论】:
以上是关于SNOWFLAKE 函数 SUM 不支持累积窗框的主要内容,如果未能解决你的问题,请参考以下文章
SQL 编译错误:无法在 Snowflake 中评估不受支持的子查询类型