Transposing and aggregating Oracle column data

*爱你&永不变心* 提交于 2020-01-11 12:32:58

问题


I have the following data

Base          End
RMSA          Item 1
RMSA          Item 2
RMSA          Item 3
RMSB          Item 1
RMSB          Item 2
RMSC          Item 4

I want to convert it to the following format

    Key           Products
    RMSA;RMSB     Item 1, Item 2
    RMSA          Item 3
    RMSC          Item 4

Basically, those with similar results should be grouped into 1 line. However, I can't seem to get it to work using listagg, etc since I'm grouping on two columns.

Is there any way to do this with a direct Oracle query?


回答1:


You can use listagg() window analytic function twice as

with t1( Base, End ) as
( 
 select 'RMSA','Item 1' from dual union all
 select 'RMSA','Item 2' from dual union all 
 select 'RMSA','Item 3' from dual union all
 select 'RMSB','Item 1' from dual union all
 select 'RMSB','Item 2' from dual union all
 select 'RMSC','Item 4' from dual 
),
   t2 as
(   
select 
       listagg(base,';') within group (order by end) 
       as key,
          end   
  from t1
 group by end 
)
select key, 
       listagg(end,',') within group (order by end) 
       as Products
  from t2  
 group by key
 order by products;

Key           Products
---------     --------------
RMSA;RMSB     Item 1, Item 2
RMSA          Item 3
RMSC          Item 4  

Demo




回答2:


Below would be one way -

WITH base 
     AS (SELECT 'RMSA'   AS base, 
                'Item 1' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSA'   AS base, 
                'Item 2' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSA'   AS base, 
                'Item 3' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSB'   AS base, 
                'Item 1' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSB'   AS base, 
                'Item 2' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSC'   AS base, 
                'Item 4' AS end1 
         FROM   dual), 
     t11 
     AS (SELECT t1.base base1, 
                t1.end1 AS end11, 
                t2.base base2, 
                t2.end1 AS end12 
         FROM   base t1 
                inner join base t2 
                        ON t1.end1 = t2.end1 
         WHERE  t1.base > t2.base) SELECT 
Concat(Concat(t11.base1, ';'), t11.base1), 
       Listagg(t11.end11, ',') 
         within GROUP (ORDER BY t11.end11) 
FROM   t11 
GROUP  BY Concat(Concat(t11.base1, ';'), t11.base1) 
--above query will get you results where you have similar results 
UNION 
SELECT t1.base, 
       t1.end1 
FROM   base t1 
       left outer join t11 
                    ON t1.base = t11.base1 
                       AND t1.end1 = t11.end11 
       left outer join t11 t12 
                    ON t1.base = t12.base2 
                       AND t1.end1 = t12.end11 
WHERE  t11.base1 IS NULL 
       AND t12.base2 IS NULL; 

Hope this helps



来源:https://stackoverflow.com/questions/54740026/transposing-and-aggregating-oracle-column-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!