组合列数据仅显示唯一值

时间:2016-10-22 20:21:46

标签: sql oracle10g

这是我的源表。我想组合列数据并仅显示唯一数据。

SQL> SELECT * FROM DUMMY;

PLAN_CD              PLAN_NM              CLASS
-------------------- -------------------- ------
D01501               DENTAL               AA
D01501               DENTAL               AB
D01501               DENTAL               AC
V01501               VISION               AA
V01501               VISION               AB

我想这样输出。

PLAN_CD              PLAN_NM              
-------              --------
D01501               DENTAL,AA,AB,AC              
V01501               VISION,AA,AB

1 个答案:

答案 0 :(得分:0)

SELECT PLAN_CD,
       LISTAGG(PLAN_NM, ',') WITHIN GROUP (ORDER BY PLAN_NM)
  FROM
    (
    SELECT PLAN_CD,
           PLAN_NM AS PLAN_NM
      FROM DUMMY
    UNION 
    SELECT PLAN_CD,
           CLASS AS PLAN_NM
      FROM DUMMY
    )
GROUP BY PLAN_CD
ORDER BY PLAN_CD;