对雪花中具有不敏感值的行进行重复数据删除

问题描述 投票:0回答:2

我想对具有不区分大小写值的行进行重复数据删除。

original table:
| ID | Name           |
| ---| -------------- |
| 1  | Apple          |
| 2  | Banana         |
| 1  | apple          |

重复数据删除后所需的输出(保持小写):

| ID | Name           |
| ---| -------------- |      
| 2  | Banana         |
| 1  | apple          |

以下语句仅适用于区分大小写的匹配。

create table DELETE2 as select distinct * from DELETE1;
drop table DELETE1;
alter table DELETE2 rename to DELETE1;

我尝试使用以下语句,但没有成功。

ALTER SESSION SET QUOTED_IDENTIFIERS_IGNORE_CASE = TRUE;

谢谢! 野泽

sql duplicates snowflake-cloud-data-platform case-insensitive
2个回答
2
投票

你可以

group by lower(x)
:

select id, max(name) name
from table
group by 1, lower(name)

0
投票

您还可以使用

COLLATE
子句。

以下代码不会根据请求将值

apple
保留为小写,无论如何,它优雅地抑制了重复:

with t(id, name) as
(
  select *
    from 
  values
     (1, 'Apple')
    ,(2, 'Banana')
    ,(1, 'apple')
)
select
   distinct
   collate(t.name, 'en-ci') as name
  ,t.* exclude (name)
from t;

结果:

| NAME   |  ID |
----------------
| Apple  |   1 |
| Banana |   2 |
© www.soinside.com 2019 - 2024. All rights reserved.