Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SQL - UNPIVOT

jstem1177Mar 25 2011 — edited Apr 12 2011
Hello All,

I'm trying to get unpivot and pivot to work and can't quite wrap my head around it.

I created a test table which contains DAY, TYPE, DB_ID. In short the backup schedule for a database instance.

Here is my SQL and output.
select b.day_abbrev, c.bck_type_abbrev
from ori_bck_schedules a
left join ori_days b on b.day_id = a.day_id
left join ori_bck_types c on c.bck_type_id = a.bck_type_id
left join ori_databases d on d.database_id = a.database_id
where a.database_id = 369;

DAY_ABBREV BCK_TYPE_ABBREV 
---------- --------------- 
SUN        FULL            
MON        INCR            
TUE        INCR            
WED        INCR            
THU        INCR            
FRI        INCR            
SAT        FULL            
I would like to unpivot this table to get 1 rows showing the day of the weekend and the type of backup? And I ultimately would like to have 1 row showing the day of the week and then just rows showing the DB_ID.
Here is what I mean:
            SUN     MON  TUE    WED  
DB1      FULL    FULL   INCR
DB2      INCR    INCR   FULL
DB3 ......
The below code is not working for me.
select b.day_abbrev as "day", c.bck_type_abbrev as "type"
from ori_bck_schedules a
left join ori_days b on b.day_id = a.day_id
left join ori_bck_types c on c.bck_type_id = a.bck_type_id
left join ori_databases d on d.database_id = a.database_id
unpivot (a for day_abbrev in (b.day_abbrev as 'D',c. bck_type_abbrev as 'T'))
;

ORA-01748: only simple column names allowed here
01748. 00000 -  "only simple column names allowed here"
*Cause:    
*Action:
Error at Line: 45 Column: 31
Can anybody help? Thanks in advance for any assistance

Jan
This post has been answered by 648982 on Apr 12 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 10 2011
Added on Mar 25 2011
9 comments
2,170 views