英文:
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';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论