Sql 雪花中的十进制精度:窗口函数与简单聚合

Sql 雪花中的十进制精度:窗口函数与简单聚合,sql,snowflake-cloud-data-platform,Sql,Snowflake Cloud Data Platform,在Snowflake中,这些查询输出相同的整数值,但小数精度不同。这是预期的行为吗 with cte (num) as (select 2356 union all select 3456 union all select 9999) select distinct avg(num) over() from cte; 输出:5270.333 with cte (num) as (select 2356 union all select 3456 union all select

在Snowflake中,这些查询输出相同的整数值,但小数精度不同。这是预期的行为吗

with cte (num) as

(select 2356 union all
 select 3456 union all
 select 9999)
 
select distinct avg(num) over()
from cte;
输出:5270.333

with cte (num) as

(select 2356 union all
 select 3456 union all
 select 9999)

select avg(num)
from cte;

输出:5270.333333

这似乎是由于文字和函数的隐式转换。可以通过显式强制转换文本值来强制一致性:

-- Returns 5270.333333333
with cte (num) as

(select 2356::float union all
 select 3456::float union all
 select 9999::float)
 
select distinct avg(num) over()
from cte;


-- Returns 5270.333333333
with cte (num) as

(select 2356::float union all
 select 3456::float union all
 select 9999::float)

select avg(num)
from cte;

Greg洞察的有趣补充:

with cte (num) as
(
  select * from values (2356::number), (3456::number), (9999::number)
)
select distinct avg(num)
from cte;
给出了5270.333333,但添加
OVER()
会将答案转换为3dp

删除distinct显示了这一点

with cte (num) as
(
  select * from values (2356::number), (3456::number), (9999::number)
)
select avg(num) OVER()
from cte;
给予:

AVG(NUM) OVER()
5270.333
5270.333
5270.333
最初,snowflake只能产生3DP的浮点数学结果,然后在2018年,他们转移到6DP。我怀疑OVER()函数是该更改的一部分,因此我建议打开一个有支持的bug报告