简体   繁体   中英

distinct listagg in Oracle sql

Please refer to the image attached.

在此输入图像描述

I have data as shown below, by using the query I am able to get the result as follow. However, I want the actual result to be distinct in Col_2. That means X1 will only appear once, followed by X2 and X3. I do not want the same value to be repeated more than once in the column.

Is it possible to achieve?

Try this way:

SELECT A.COL_1, 
       LISTAGG(A.COL_2, ',') 
          WITHIN GROUP(ORDER BY A.COL_2) AS COL_2, 
       SUM(A.SS) AS COL_3
  FROM (SELECT COL_1, COL_2, SUM(COL_3) SS
          FROM TEST
         GROUP BY COL_1, COL_2 ) A
 GROUP BY COL_1

See it here on fiddle: http://sqlfiddle.com/#!4/ee5f2/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM