m@rc-lenzin

Wenn etwas schief geht, dann freu dich. Das sind die Geschichten die dir in Erinnerung bleiben werden

Oracle Pivot

 

/** ********************** **
 ** tabelle: PIVOT_TEST
 ** 
 **  ID_    KEY_    VAL_
 **  ------ ------- -------
 **  1      size    2
 **  1      color   blue
 **  1      height  6
 **  2      size    3
 **  2      color   red
 **  3      size    4
 ** ********************** **/

-- Create Basic
create table pivot_test
    (
    id_     number(1)       not null,
    key_    varchar2(20)    not null,
    val_    varchar2(200)   not null     
    );

insert into pivot_test(id_,key_,val_) values(1, 'size',     '2');
insert into pivot_test(id_,key_,val_) values(1, 'color',    'blue');
insert into pivot_test(id_,key_,val_) values(1, 'height',   '6');
insert into pivot_test(id_,key_,val_) values(2, 'size',     '3');
insert into pivot_test(id_,key_,val_) values(2, 'color',    'red');
insert into pivot_test(id_,key_,val_) values(3, 'size',     '4');

commit;

-- PIVOT mit einem SELECT
select  
      id_, 
      max(decode(key_, 'size',      val_)) "SIZE",
      max(decode(key_, 'color',     val_)) "COLOR",
      max(decode(key_, 'height',    val_)) "HEIGHT"
  FROM pivot_test
  GROUP BY id_;
      
-- PIVOT mit ORACLE
select  distinct * 
  from (select  id_,
                key_,
                val_
          from  pivot_test)
  pivot
    ( 
    min(val_)
    for key_ in ('size','color','height')
    )

Weiter Beitrag

Zurück Beitrag

© 2025 m@rc-lenzin

Thema von Anders Norén