Oracle listagg去重distinct的方式有哪些

avatar
作者
猴君
阅读量:0

在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; 

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

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!