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

UNPIVOT su più colonne per restituire più colonne

così:

SQL> select * from network_table;

 ORIG_CODE    ORIG_SV  DEST_CODE    DEST_SV
---------- ---------- ---------- ----------
        14          1         15          1
        12          2         22          2
        18          4         11          1
        15          1         22          3
        14          3         11          1

SQL> select case name when 'ORIG_SV' then orig_code else dest_code end code, val 
  2  from network_table 
  3  unpivot (val for name in (orig_sv, dest_sv));

      CODE        VAL
---------- ----------
        14          1
        15          1
        12          2
        22          2
        18          4
        11          1
        15          1
        22          3
        14          3
        11          1

o 10 ge inferiore:

SQL> select case  r when 1 then orig_code else dest_code end code,
  2         case r when 1 then orig_sv else dest_sv end val
  3    from network_table, (select rownum r from dual connect by level <= 2)
  4  /

      CODE        VAL
---------- ----------
        14          1
        12          2
        18          4
        15          1
        14          3
        15          1
        22          2
        11          1
        22          3
        11          1