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

Converti righe in colonne Oracle

prova questo per generare uno script:

select 'select '||FILE_ID||' FILE_ID,'||
  ltrim(sys_connect_by_path('REC_FLD_'||FIELD_NUMBER||' "'||FIELD_NAME||'"',','),',')||
  ' from RESPONSE_DETAILS where FILE_ID=' ||FILE_ID||';'
  from (select t.*,count(*) over (partition by FILE_ID) cnt from RESPONSE_METADATA t)
 where cnt=FIELD_NUMBER start with FIELD_NUMBER=1 
connect by prior FILE_ID=FILE_ID and prior FIELD_NUMBER=FIELD_NUMBER-1

Aggiunte virgolette doppie perché alcuni nomi di colonna sono riservati "DESC", ad esempio