如何删除配置单元字符串中的重复项?

Arp*_*edi 1 hadoop hive

我有用重复值分隔的column(string)逗号。我要删除重复项:
例如

column_name
-----------------
枪,枪,人,枪,人
班车,敌人,敌人,奔跑
,追逐

我想要这样的结果:

column_name
----------------
枪,
穿梭,敌人,奔跑
,追逐

我正在使用蜂巢数据库。请帮忙。

Dav*_*itz 5

选项1:保留最后一次出现

这将保留每个单词的最后一次出现。
例如'hello,world,hello,world,hello'将导致'world,hello'

select  regexp_replace
        (
            column_name
           ,'(?<=^|,)(?<word>.*?),(?=.*(?<=,)\\k<word>(?=,|$))'
           ,''
        )

from    mytable
;
Run Code Online (Sandbox Code Playgroud)
+-------------------+
| gun,man           |
| shuttle,enemy,run |
| hit,chase         |
+-------------------+
Run Code Online (Sandbox Code Playgroud)

选项2:保持首次出现

这将保留每个单词的第一个出现。
例如'hello,world,hello,world,hello'将导致'hello,world'

select  reverse            
        (
            regexp_replace
            (
                reverse(column_name)
               ,'(?<=^|,)(?<word>.*?),(?=.*(?<=,)\\k<word>(?=,|$))'
               ,''
            )
        )

from    mytable
;
Run Code Online (Sandbox Code Playgroud)

选项3:已排序

例如'Cherry,Apple,Cherry,Cherry,Cherry,Banana,Apple'将导致'Apple,Banana,Cherry'

select  regexp_replace
        (
            concat_ws(',',sort_array(split(column_name,',')))
           ,'(?<=^|,)(?<word>.*?)(,\\k<word>(?=,|$))+'
           ,'${word}'
        )

from    mytable
;
Run Code Online (Sandbox Code Playgroud)