Oracle
 sql >> Database >  >> RDS >> Oracle

Oracle LISTAGG() per più attributi?

select K, listagg(case when chgv1 = 1 then v1 else null end,',') within group (order by v1) as v1lst, --Only consider cases in listagg when rows have changed
          listagg(case when chgv2 = 1 then v2 else null end,',') within group (order by v2) as v2lst  --Only consider cases in listagg when rows have changed
from (
 select k, v1, v2,
        row_number() over (partition by k,v1 order by v1 ) as chgv1, --Detect changes in v1. In this case it's 1.
        row_number() over (partition by k,v2 order by v1 ) as chgv2  --Detect changes in v1. In this case it's 1.
 from t)
group by k;