[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Oracle Pivot Query

Posted on 2010-11-24
Medium Priority
Last Modified: 2013-12-07
I am trying to write a pivot query in Oracle 11g.

cust_id          date              prod_id
10001701      25/03/2003      2
10001701      25/03/2003      1
10001701      25/03/2003      5

cust_id          date              prod_id_1    prod_id_2 prod_id_3 prod_id_4 prod_id_5
10001701      25/03/2003    1                 1                0               0               1

There wont be duplicate record for prod_id againt cust_id.

Question by:sachin_dba
LVL 58

Accepted Solution

cyberkiwi earned 2000 total points
ID: 34210464
select cust_id, date,
sum(case when prod_id=1 then 1 else 0 end) prod_id_1,
sum(case when prod_id=2 then 1 else 0 end) prod_id_2,
sum(case when prod_id=3 then 1 else 0 end) prod_id_3,
sum(case when prod_id=4 then 1 else 0 end) prod_id_4,
sum(case when prod_id=5 then 1 else 0 end) prod_id_5
from tbl
group by cust_id, date
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34210469
I do not have 11g at the moment.

The below links should help on how to do that easily. Let me know if you cannot do it. Give me the pivot query which you have it any and i can modify it.



Expert Comment

ID: 34210472
in 11G there is an pivot option, but i am not sure how that works..

but without using any pivot option and if your columns are fixed you can do this,

select cust_id, trunc(date),  min(prd_id_1), min(prd_id_2), min(prd_id_3), min(prd_id_4), min(prd_id_5) from
select cust_id, date,
decode(prdid, 1, 1, 0) prd_id_1,
decode(prdid, 2, 1, 0) prd_id_2
decode(prdid, 3, 1, 0) prd_id_3
decode(prdid, 4, 1, 0) prd_id_4
decode(prdid, 5, 1, 0) prd_id_5 from table)
group by cust_id, trunct(date);
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

LVL 28

Expert Comment

by:Naveen Kumar
ID: 34210522
Just a note, you can use the solutions given by cyberwiki if your data has a maximum of only 5 prod codes. If you have 20 prod codes, then you may have to modify the query and modify for next 15 prod codes to make it for you.

If your data has unknown number of prod codes or is dynamic, then that solution cannot work for you and you have try to use the 11g pivot operator.

LVL 58

Expert Comment

ID: 34210570

Are you sure the 11g pivot operator can handle dynamic number of columns?

Seems to imply same as SQL Server, following ansi standard, the IN list is fixed.
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34210599

I agree to what you said as the fact the IN list for the PIVOT has to be hard coded there and that is why i said in my previous update "you have to try to use the 11g pivot operator".

As i do not have the 11g version database, i cannot confirm whether my try can work or not. But most likely No will be answer.

LVL 28

Expert Comment

by:Naveen Kumar
ID: 34210607
see this link :

We can use ANY in the IN operator or use a sub query there to get the prod codes dynamically.



Author Closing Comment

ID: 34210687
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34210723

I am fine with you in accepting the answer you like. But then i do not understand why you mentioned that "I am trying to write a pivot query in Oracle 11g." while creating this quesion. This gives an impression that you need help for a pivot query in 11g but the answer which is now accepted does not use the pivot query in 11g.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question