雪花字符串列与日期列比较

huangapple go评论60阅读模式
英文:

snowflake string column vs date column comparison

问题

  1. 第一个查询返回正确结果,它比较日期列(eff_bgn_dt)与字符串,我认为其他数据库不允许字符串与日期列进行比较,应该是字符串与字符串或日期与日期之间的比较,那么在Snowflake中进行这种比较会发生什么情况?是比较哈希值吗?

  2. 第二个查询是将字符串与日期列(eff_bgn_dt)进行比较,然后按日期列进行分组,并统计每个分组的记录数。

英文:

I have 2 queries below , both return results , the first one returns correct results, the second query compares string against date column(eff_bgn_dt), I think other databases wont allow string to date comparison, either should be string vs string or date vs date, what exactly happens with such comparison in snowflake?, compares hash value ?

1.select count(*)
from TableA
where  TO_DATE('20230605', 'YYYYMMDD') >eff_bgn_dt ;

 

2.select eff_bgn_dt,count(*)
from TableA
where  '20230605' >eff_bgn_dt group by eff_bgn_dt;

答案1

得分: 0

I think other databases won't allow string to date comparison,

Depending on RDBMS they allow such comparison by performing "implicit conversion" - 数据类型优先级


Snowflake 隐式转换("强制"):

> 雪花字符串列与日期列比较

警告 - 这种隐式转换可能会有点误导,因为值 20230605 不被视为 'YYYYMMDD':

select '20230605'::DATE;
-- 1970-08-23

select '2023-06-05'::DATE;
-- 2023-06-05

日期
>
>如果输入参数的格式是包含整数的字符串:
>
> - 将字符串转换为整数后,该整数将被视为从Unix纪元(1970-01-01 00:00:00.000000000 UTC)开始的秒数、毫秒数、微秒数或纳秒数。


实际查询:

select eff_bgn_dt,count(*)
from TableA
where  '20230605' > eff_bgn_dt 
group by eff_bgn_dt;

<=>

select eff_bgn_dt,count(*)
from TableA
where  '20230605'::DATE > eff_bgn_dt 
group by eff_bgn_dt;

<=>

select eff_bgn_dt,count(*)
from TableA
where  TO_DATE('1970-08-23', 'YYYY-MM-DD') > eff_bgn_dt 
group by eff_bgn_dt;
英文:

> I think other databases wont allow string to date comparison,

Depending on RDBMS they allow such comparison by performing "implicit conversion" - Data type precedence


Snowflake Implicit Casting (“Coercion”):

> 雪花字符串列与日期列比较

Warning - such implicit conversion may be a bit misleading because value 20230605 is not treated as 'YYYYMMDD':

select '20230605'::DATE;
-- 1970-08-23

select '2023-06-05'::DATE;
-- 2023-06-05

DATE
>
>If the format of the input parameter is a string that contains an integer:
>
> - After the string is converted to an integer, the integer is treated as a number of seconds, milliseconds, microseconds, or nanoseconds after the start of the Unix epoch (1970-01-01 00:00:00.000000000 UTC).


The actual query:

select eff_bgn_dt,count(*)
from TableA
where  '20230605' > eff_bgn_dt 
group by eff_bgn_dt;

<=>

select eff_bgn_dt,count(*)
from TableA
where  '20230605'::DATE > eff_bgn_dt 
group by eff_bgn_dt;

<=>

select eff_bgn_dt,count(*)
from TableA
where  TO_DATE('1970-08-23', 'YYYY-MM-DD') > eff_bgn_dt 
group by eff_bgn_dt;

huangapple
  • 本文由 发表于 2023年6月12日 23:42:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定