温馨提示×

Oracle listagg去重distinct的方式有哪些

小亿
419
2024-03-02 19:48:29
栏目: 云计算

在Oracle中,可以使用不同的方法来实现在使用LISTAGG函数时去重重复的值。以下是一些常用的方法:

  1. 使用子查询和ROW_NUMBER()函数:
SELECT LISTAGG(column_name, ',') WITHIN GROUP (ORDER BY column_name) AS result
FROM (
    SELECT column_name, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY column_name) rn
    FROM table_name
)
WHERE rn = 1;
  1. 使用GROUP BY子句:
SELECT LISTAGG(DISTINCT column_name, ',') WITHIN GROUP (ORDER BY column_name) AS result
FROM table_name;
  1. 使用CONNECT BY子句:
SELECT LISTAGG(column_name, ',') WITHIN GROUP (ORDER BY column_name) AS result
FROM (
    SELECT column_name, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY column_name) rn
    FROM table_name
)
START WITH rn = 1
CONNECT BY PRIOR column_name = column_name AND PRIOR rn = rn-1;

这些方法可以根据具体需求选择合适的方式来去重重复的值。

0