简体   繁体   English

Oracle sql中的distinct listagg

[英]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. 但是,我希望实际结果在Col_2中是不同的。 That means X1 will only appear once, followed by X2 and X3. 这意味着X1只出现一次,然后是X2和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 在小提琴上看到它: http ://sqlfiddle.com/#!4 / ie5f2 / 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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