阅读量:0
在Oracle中,可以使用不同的方法来实现在使用LISTAGG函数时去重重复的值。以下是一些常用的方法:
- 使用子查询和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;
- 使用GROUP BY子句:
SELECT LISTAGG(DISTINCT column_name, ',') WITHIN GROUP (ORDER BY column_name) AS result FROM table_name;
- 使用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;
这些方法可以根据具体需求选择合适的方式来去重重复的值。