sql >> Database >  >> RDS >> Oracle

Transponeren en aggregeren van Oracle-kolomgegevens

U kunt listagg() . gebruiken venster analytische functie tweemaal als

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



  1. #1005 - Kan tabel 'table_name' niet maken (errno:150)

  2. mysql-transactie - terugdraaien op elke uitzondering

  3. Tel het aantal overlappende elementen in de Postgres-array

  4. Top 10 methoden om ETL-prestaties te verbeteren met behulp van SSIS