如何将逗号更改为竖线并删除重复项,然后修剪LISTAGG中的最后分隔符。

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

How to change comma to pipe and remove duplicates and trim last delimiter in LISTAGG

问题

  1. |ID|USER_NAME |
  2. |--|------------|
  3. |1 |A1|B12|C32 |
  4. |2 |A1 |
  5. |3 |B12|C32 |
英文:

I have a query which result is: (EDIT: 13.06.2023 12:30)

  1. |ID|USER_NAME |
  2. |--|--------------------|
  3. |1 |A1,A1,A1,B12,B12,C32|
  4. |2 |A1 |
  5. |3 |B12,C32 |

Query for the above result:

  1. SELECT
  2. ID,
  3. LISTAGG (user_name, ',') WITHIN GROUP (ORDER BY user_name )
  4. From my_table
  5. GROUP BY ID;

What I have tried to replace ',' with '|' and remove the duplicates and trim the last delimiter is:

  1. SELECT
  2. ID,
  3. RTRIM(REGEXP_REPLACE(LISTAGG (user_name, ',') WITHIN GROUP (ORDER BY user_name ), '([^,]+)(,|)+', '|'),'|')
  4. From my_table
  5. GROUP BY ID;

The output is:

  1. |ID|USER_NAME |
  2. |--|------------|
  3. |1 |A1|2|B12|C32|
  4. |2 |A1 |
  5. |3 |B12|C32 |

EDIT:
The LISTAGG and REGEXP should be part of a longer query and not be a standalone SELECT (output of LISTAGG should be a column of a long query). The example should only show what output I want to have in the end.<br />

The second value for user_name and ID 1 is not correct.<br />
How can I get the correct values? What is wrong in the query?<br />
Thank you.

答案1

得分: 2

首先选择不重复的数值,然后将 listagg 应用于已经不重复的数据集:

  1. WITH
  2. temp
  3. AS
  4. (SELECT DISTINCT id, user_name
  5. FROM my_table)
  6. SELECT id,
  7. LISTAGG (user_name, '|') WITHIN GROUP (ORDER BY user_name)
  8. FROM temp
  9. GROUP BY id;
英文:

First select distinct values, and then apply listagg to already distinct data set:

  1. WITH
  2. temp
  3. AS
  4. (SELECT DISTINCT id, user_name
  5. FROM my_table)
  6. SELECT id,
  7. LISTAGG (user_name, &#39;|&#39;) WITHIN GROUP (ORDER BY user_name)
  8. FROM temp
  9. GROUP BY id;

答案2

得分: 0

  1. 您可以使用 `collect` 聚合函数来获取不重复的值,然后在集合上使用 `listagg` 函数。
  2. ```sql
  3. create table sample_table (id int, user_name varchar2(10))
  1. begin
  2. insert into sample_table values(1, 'A1');
  3. insert into sample_table values(1, 'A1');
  4. insert into sample_table values(1, 'A1');
  5. insert into sample_table values(1, 'B12');
  6. insert into sample_table values(1, 'B12');
  7. insert into sample_table values(1, 'C32');
  8. insert into sample_table values(2, 'A1');
  9. insert into sample_table values(3, 'B12');
  10. insert into sample_table values(3, 'C32');
  11. insert into sample_table values(3, 'B12');
  12. commit;
  13. end;
  1. with grouped as (
  2. select
  3. id,
  4. cast(collect(distinct user_name) as sys.odcivarchar2list) as qwe
  5. from sample_table
  6. group by id
  7. )
  8. select
  9. id,
  10. (
  11. select
  12. listagg(column_value, '|') within group(order by column_value) as user_name
  13. from table(grouped.qwe)
  14. ) as user_name
  15. from grouped
ID USER_NAME
1 A1
2 A1
3 B12

fiddle

  1. <details>
  2. <summary>英文:</summary>
  3. You may use `collect` aggregation to get distinct values and then use `listagg` over collection.

create table sample_table (id int, user_name varchar2(10))

begin
insert into sample_table values(1, 'A1');
insert into sample_table values(1, 'A1');
insert into sample_table values(1, 'A1');
insert into sample_table values(1, 'B12');
insert into sample_table values(1, 'B12');
insert into sample_table values(1, 'C32');
insert into sample_table values(2, 'A1');
insert into sample_table values(3, 'B12');
insert into sample_table values(3, 'C32');
insert into sample_table values(3, 'B12');
commit;
end;/

with grouped as (
select
id,
cast(collect(distinct user_name) as sys.odcivarchar2list) as qwe
from sample_table
group by id
)
select
id,
(
select
listagg(column_value, '|') within group(order by column_value) as user_name
from table(grouped.qwe)
) as user_name
from grouped

  1. | ID | USER\_NAME |
  2. | --:|:---------|
  3. | 1 | A1\|B12\|C32 |
  4. | 2 | A1 |
  5. | 3 | B12\|C32 |
  6. [fiddle](https://dbfiddle.uk/b-BRd_QD)
  7. </details>

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

发表评论

匿名网友

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

确定