雪花连接是否不区分大小写

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

Is snowflake join case insensitive

问题

我试图连接两个表,但似乎连接不考虑大小写。Snowflake中的连接是否不区分大小写?

以下查询是不区分大小写地连接的

with cte_1 as 
(select  VIN,YEAR,MAKENAME,MODELNAME from TBL_A),

cte_2 as 
(select YEAR,MAKE_CHROME,MODEL_CHROME from TBL_B)

select * from 
cte_1 a 
inner join cte_2 b on 
    a.YEAR=b.year and 
    a.MAKENAME=b.MAKE_CHROME and 
    a.MODELNAME=b.MODEL_CHROME;

雪花连接是否不区分大小写

英文:

I am trying to join two tables but seems like the join is not considering case. Is the join in snowflake case insensitive?

Following query is being joined case insensitively

with cte_1 as 
(select  VIN,YEAR,MAKENAME,MODELNAME from TBL_A),

cte_2 as 
(select YEAR,MAKE_CHROME,MODEL_CHROME from TBL_B)

select * from 
cte_1 a 
inner join cte_2 b on 
    a.YEAR=b.year and 
    a.MAKENAME=b.MAKE_CHROME and 
    a.MODELNAME=b.MODEL_CHROME;

雪花连接是否不区分大小写

答案1

得分: 0

根据NickW的评论,字符串默认情况下没有匹配,因此:

with cte_1(YEAR, MAKENAME, MODELNAME) as (
    select * from values
        (2022, 'FORD', 'BRONCO')
), cte_2(YEAR, MAKE_CHROME, MODEL_CHROME) as (
    select * from values
        (2022, 'Ford', 'Bronco')
)
select * 
from cte_1 as a  
join cte_2 b 
    on a.YEAR=b.year 
        and a.MAKENAME = b.MAKE_CHROME 
        and a.MODELNAME = b.MODEL_CHROME;

没有结果:

雪花连接是否不区分大小写

所以如果我们使用COLLATE更改数据类型,它现在将“匹配”

with cte_1(YEAR, MAKENAME, MODELNAME) as (
    select 
        column1
        ,COLLATE(column2, 'en-ci')
        ,COLLATE(column3, 'en-ci')
    from values
        (2022, 'FORD', 'BRONCO')
), cte_2(YEAR, MAKE_CHROME, MODEL_CHROME) as (
    select
        column1
        ,COLLATE(column2, 'en-ci')
        ,COLLATE(column3, 'en-ci')
    from values
        (2022, 'Ford', 'Bronco')
)
select * 
from cte_1 as a  
join cte_2 b 
    on a.YEAR=b.year 
        and a.MAKENAME=b.MAKE_CHROME 
        and a.MODELNAME=b.MODEL_CHROME;

给出结果:

雪花连接是否不区分大小写

这意味着当创建表时,它为列集合设置了排序规则

create table TBL_A(
    YEAR number, 
    MAKENAME text COLLATE 'en-ci',
    MODELNAME text COLLATE 'en-ci');

insert into tbl_a values (2022, 'FORD', 'BRONCO');

with cte_1 as (
    select * from TBL_A
), cte_2(YEAR, MAKE_CHROME, MODEL_CHROME) as (
    select
        column1
        ,COLLATE(column2, 'en-ci')
        ,COLLATE(column3, 'en-ci')
    from values
        (2022, 'Ford', 'Bronco')
)
select * 
from cte_1 as a  
join cte_2 b 
    on a.YEAR=b.year 
        and a.MAKENAME=b.MAKE_CHROME 
        and a.MODELNAME=b.MODEL_CHROME;

因此,要查找这个信息,我们可以查看信息模式中的COLUMNS视图

select 
    table_name,
    column_name,
    data_type,
    collation_catalog,
    collation_name,
    collation_schema
from information_schema.columns
where table_name = 'TBL_A';

雪花连接是否不区分大小写

英文:

As per NickW comment, by default strings have no matching, thus:


with cte_1(YEAR, MAKENAME, MODELNAME) as (
    select * from values
        (2022, 'FORD', 'BRONCO')
), cte_2(YEAR, MAKE_CHROME, MODEL_CHROME) as (
    select * from values
        (2022, 'Ford', 'Bronco')
)
select * 
from cte_1 as a  
join cte_2 b 
    on a.YEAR=b.year 
        and a.MAKENAME = b.MAKE_CHROME 
        and a.MODELNAME = b.MODEL_CHROME;

gives no results:

雪花连接是否不区分大小写

So if we change the data type with COLLATE it will now "match"

with cte_1(YEAR, MAKENAME, MODELNAME) as (
    select 
        column1
        ,COLLATE(column2, 'en-ci')
        ,COLLATE(column3, 'en-ci')
    from values
        (2022, 'FORD', 'BRONCO')
), cte_2(YEAR, MAKE_CHROME, MODEL_CHROME) as (
    select
        column1
        ,COLLATE(column2, 'en-ci')
        ,COLLATE(column3, 'en-ci')
    from values
        (2022, 'Ford', 'Bronco')
)
select * 
from cte_1 as a  
join cte_2 b 
    on a.YEAR=b.year 
        and a.MAKENAME=b.MAKE_CHROME 
        and a.MODELNAME=b.MODEL_CHROME;

gives:

雪花连接是否不区分大小写

This implies that when your table was created it had collation for the column set

create table TBL_A(
    YEAR number, 
    MAKENAME text COLLATE 'en-ci',
    MODELNAME text COLLATE 'en-ci');

insert into tbl_a values (2022, 'FORD', 'BRONCO');

with cte_1 as (
    select * from TBL_A
), cte_2(YEAR, MAKE_CHROME, MODEL_CHROME) as (
    select
        column1
        ,COLLATE(column2, 'en-ci')
        ,COLLATE(column3, 'en-ci')
    from values
        (2022, 'Ford', 'Bronco')
)
select * 
from cte_1 as a  
join cte_2 b 
    on a.YEAR=b.year 
        and a.MAKENAME=b.MAKE_CHROME 
        and a.MODELNAME=b.MODEL_CHROME;

so to find that out we can look at the COLUMNS view in the information schema:

select 
    table_name,
    column_name,
    data_type,
    collation_catalog,
    collation_name,
    collation_schema
from information_schema.columns
where table_name = 'TBL_A';

雪花连接是否不区分大小写

huangapple
  • 本文由 发表于 2023年7月13日 19:25:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678842.html
匿名

发表评论

匿名网友

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

确定