如何在select子句中使用多个LISTAGG()函数时选择不同的值?

时间:2015-07-16 12:15:13

标签: sql oracle

我正在尝试使用以下查询检索race_code,chara_code和reason_code作为列表:

SELECT a.pid,
       LISTAGG(a.rc, ',') WITHIN GROUP (ORDER BY a.rc) AS race,
       LISTAGG(a.cc, ',') WITHIN GROUP (ORDER BY a.cc) as chara_codes,
       LISTAGG(a.rrc, ',') WITHIN GROUP (ORDER BY a.rrc) AS removal_reason
FROM (
   SELECT UNIQUE
          p.person_id pid,
          r.race_code rc,
          c.characteristic_code cc,
          rr.removal_reason_code rrc
     FROM person p left outer join race r on p.person_id = r.person_id
          left outer join characteristic c on p.person_id = c.person_id
          left outer join placement_episode pe on p.person_id = pe.child_id
          left outer join removal_reason rr on pe.placement_episode_id = rr.placement_episode_id
     ) a
GROUP BY a.pid

在引用link1link2等链接后,我尝试了此查询。但在这样做之后,我也无法获得所有字段的唯一值。

我的o / p就像:

pid      race_code     chara_code      reason_code
 1        a,b,b,c     c1,c1,c2,c3     r1,r2,r3,r3
 2       a,c,d,d,d      c1,c2,c2        r3,r3

and so on.

如果我尝试一次只检索一个字段并保持所需的连接操作,那么它会给出正确的结果。但是对于多个LISTAGG()函数,它的重复值 我没有办法做到这一点。有没有其他方法可以获得不同的价值观?

2 个答案:

答案 0 :(得分:4)

遗憾的是,这比需要的更复杂。但是,你可以做到。我们的想法是枚举每个值,然后使用caseNULL参数传递给LISTAGG()

SELECT a.pid,
       LISTAGG(CASE WHEN rc_seqnum = 1 THEN a.rc END, ',') WITHIN GROUP (ORDER BY a.rc) AS race,
       LISTAGG(CASE WHEN cc_seqnum = 1 THEN a.cc END, ',') WITHIN GROUP (ORDER BY a.cc) as chara_codes,
       LISTAGG(CASE WHEN rrc_seqnum = 1 THEN a.rrc END, ',') WITHIN GROUP (ORDER BY a.rrc) AS removal_reason
FROM (SELECT p.person_id as pid, r.race_code as rc, c.characteristic_code as cc,
              rr.removal_reason_code as rrc,
             row_number() over (partition by p.person_id, r.race_code order by r.race_code) as rc_seqnum,
             row_number() over (partition by p.person_id, c.characteristic_code order by c.characteristic_code) as cc_seqnum,
             row_number() over (partition by p.person_id, rr.removal_reason_code order by rr.removal_reason_code) as rrc_seqnum
      FROM person p left outer join race r on p.person_id = r.person_id
           left outer join characteristic c on p.person_id = c.person_id
           left outer join placement_episode pe on p.person_id = pe.child_id
           left outer join removal_reason rr on pe.placement_episode_id = rr.placement_episode_id
     ) a
GROUP BY a.pid;

查询基于每个人和字段组合枚举行。第一次看到该值时,它的值为" 1",后续值逐渐增加。 LISTAGG()仅选择第一个值。

您应该了解分析功能。它们非常有用。

答案 1 :(得分:0)

您可以使用标量子查询,如下例所示:

select p.person_id
     , (select LISTAGG(rc, ',') WITHIN GROUP (ORDER BY rc) 
          from race r where r.person_id = p.person_id
         group by r.person_id) race
     , (select LISTAGG(cc, ',') WITHIN GROUP (ORDER BY cc) 
          from characteristic r where r.person_id = p.person_id
         group by r.person_id) chara_codes
     , (select LISTAGG(rrc, ',') WITHIN GROUP (ORDER BY rrc) 
          from removal_reason r where r.person_id = p.person_id
         group by r.person_id) removal_reason
  from person p;

或者您可以预先计算列表,如下例所示:

with race_list as (
  select person_id
       , LISTAGG(rc, ',') WITHIN GROUP (ORDER BY rc) race
    from race
   group by person_id
), characteristic_list as (
  select person_id
       , LISTAGG(cc, ',') WITHIN GROUP (ORDER BY cc) chara_codes
    from characteristic
   group by person_id
), removal_reason_list as (
  select person_id
       , LISTAGG(rrc, ',') WITHIN GROUP (ORDER BY rrc) removal_reason
    from removal_reason
   group by person_id
)
select p.person_id
     , r.race
     , c.chara_codes
     , rr.removal_reason
  from person p
  join race_list r
    on r.person_id = p.person_id
  join characteristic_list c
    on c.person_id = p.person_id
  join removal_reason_list rr
    on rr.person_id = p.person_id;