Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, kushal.

Asked: July 29, 2016 - 7:59 pm UTC

Last updated: August 01, 2016 - 3:17 pm UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi Tom,

Please consider below scenario.

Table A

ID Attribute Value

1 'X' 3
1 'Y' 4
2 'X' 5
3 'X' 6
3 'Y' 7
3 'Z' 8
3 'A' 9

Expected output

ID X Y Z A
1 3 4 null null
2 5 null null null
3 6 7 8 9


It is possible to achieve as i know only basic level of pivoting?? how can we achieve this?
Thank you in advance


and Chris said...

Yes, just write your pivot out like normal. Missing values appear as null:

create table t (
  ID int,Attribute varchar2(1),Value int
);
insert into t values (1,'X',3);
insert into t values (1,'Y',4);
insert into t values (2,'X',5);
insert into t values (3,'X',6);
insert into t values (3,'Y',7);
insert into t values (3,'Z',8);
insert into t values (3,'A',9);
commit;

select * from t
pivot (
  min(value) for attribute in (
    'X' as x, 'Y' as y, 'Z' as z, 'A' as a
  )
);

ID  X  Y  Z  A  
1   3  4        
2   5           
3   6  7  8  9 

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, August 01, 2016 - 6:40 pm UTC