Sql 如何消除列中的重复数据
我想消除重复和显示一次 比如说Sql 如何消除列中的重复数据,sql,regex,oracle,oracle11g,Sql,Regex,Oracle,Oracle11g,我想消除重复和显示一次 比如说 SELECT 'apple, apple, orange' FROM dual; 我想展示一下 apple, orange 再举一个例子 SELECT 'apple, apple, apple, apple,' FROM dual; 我只是想展示一下 apple 此代码显示 with data as ( select 'apple, apple, apple, apple' col from dual ) select listagg(col, ','
SELECT 'apple, apple, orange'
FROM dual;
我想展示一下
apple, orange
再举一个例子
SELECT 'apple, apple, apple, apple,'
FROM dual;
我只是想展示一下
apple
此代码显示
with data as
(
select 'apple, apple, apple, apple' col from dual
)
select listagg(col, ',') within group(order by 1) col
from (
select distinct regexp_substr(col, '[^,]+', 1, level) col
from data
connect by level <= regexp_count(col, ',')
)
这样可以消除重复项: 输出 正如你所看到的,你会得到一个空值,因为额外的逗号
as中有几个选项: 只需将数字替换为输入“apple,apple,orange”使用trim和distinct with regexp函数,这对于获得所需结果非常重要
select listagg(str,',') within group (order by 0) as Result
from
(
select distinct trim(regexp_substr('apple, apple, orange','[^,]+', 1, level)) as str
from dual
connect by level <= regexp_count('apple, apple, orange',',') + 1
);
为什么标题中有日期?@user7294900抱歉,这意味着重复数据您可以编辑您的问题title@JuanCarlosOropeza. 谢谢我做到了!!!你能检查一下你对rextester的查询吗?他没有给我预期的输出
with data as
(
select
'5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' col from dual
)
select listagg(col, ',') within group(order by 1) col
from (
select distinct regexp_substr(col, '[^,]+', 1, level) col
from data
connect by level <= regexp_count(col, ',')
)
select listagg(str,',') within group (order by 0) as Result
from
(
select distinct trim(regexp_substr('apple, apple, orange','[^,]+', 1, level)) as str
from dual
connect by level <= regexp_count('apple, apple, orange',',') + 1
);