PL / SQL Developer删除重复的条目,但保留相关的列数据

时间:2013-03-18 15:17:18

标签: oracle plsql

您好我有一个查询,我需要删除或格式化代码的输出显示。请参阅以下内容:

查询:

select rfc."NUMBER",
ag.current_pending_groups
from smspadm.cm3rm1 rfc, smspadm.tv_approval_vw appr, smspadm.approvala1 ag
Order By rfc."NUMBER"

目前的结果:

NUMBER  CURRENT_PENDING_GROUPS

C301609 Comm/AT&T Vert DCAB
C302023 Collections DCAB
C302023 Comm/AT&T Vert DCAB
C302023 Fin-D2C-DNB DCAB
C302023 Interactive Recording DCAB
C302103 Collections DCAB
C302103 Wintel Server DCAB
C302103 Fin-D2C-DNB DCAB
C301813 GTHRA Vert DCAB
C301813 Tech Vert DCAB

期望的结果: 要么:

NUMBER  CURRENT_PENDING_GROUPS

C301609 Comm/AT&T Vert DCAB
C302023 Collections DCAB, Comm/AT&T Vert DCAB, Fin-D2C-DNB DCAB
C302103 Collections DCAB, Wintel Server DCAB, Fin-D2C-DNB DCAB
C301813 GTHRA Vert DCAB, Tech Vert DCAB

或者它应该是:

NUMBER  CURRENT_PENDING_GROUPS

C301609 Comm/AT&T Vert DCAB
C302023 Collections DCAB
        Comm/AT&T Vert DCAB
        Fin-D2C-DNB DCAB
        Interactive Recording DCAB
C302103 Collections DCAB
        Wintel Server DCAB
        Fin-D2C-DNB DCAB
C301813 GTHRA Vert DCAB
        Tech Vert DCAB

我应该使用什么查询来获得此结果?

2 个答案:

答案 0 :(得分:1)

SELECT rfc."NUMBER",
  LISTAGG(ag.current_pending_groups, ', ')
   WITHIN GROUP (ORDER BY rfc."NUMBER") "CURRENT_PENDING_GROUPS"
   FROM smspadm.cm3rm1 rfc, smspadm.tv_approval_vw appr, smspadm.approvala1 ag
   GROUP BY rfc."NUMBER";


SELECT rfc."NUMBER",
   wm_concat(ag.current_pending_groups)
   FROM smspadm.cm3rm1 rfc, smspadm.tv_approval_vw appr, smspadm.approvala1 ag
   GROUP BY rfc."NUMBER";

答案 1 :(得分:0)

第一个变种

select 
  rfc."NUMBER",
  wm_concat(ag.current_pending_groups)  -- for 10g
  -- listagg(ag.current_pending_groups,',') within group (order by ag.current_pending_groups)  -- for 11g
  as CURRENT_PENDING_GROUPS
from smspadm.cm3rm1 rfc, smspadm.tv_approval_vw appr, smspadm.approvala1 ag
group by rfc."NUMBER"
Order By rfc."NUMBER"

第二个变种

select
  case when
    row_number() over (partition by rfc."NUMBER" order by ag.current_pending_groups) = 1
  then
    rfc."NUMBER"
  end as "NUMBER",
  ag.current_pending_groups
from smspadm.cm3rm1 rfc, smspadm.tv_approval_vw appr, smspadm.approvala1 ag
Order By rfc."NUMBER", ag.current_pending_groups