?
Solved

Oracle Pivot Query

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

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

Output
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.

0
Comment
Question by:sachin_dba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 58

Accepted Solution

by:
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
0
 
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.

http://www.oracle-base.com/articles/11g/PivotAndUnpivotOperators_11gR1.php

thanks
0
 
LVL 3

Expert Comment

by:mpaladugu
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);
0
Technology Partners: 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!

 
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.

Thanks
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34210570
nav_kum_v

Are you sure the 11g pivot operator can handle dynamic number of columns?
http://www.oracle-developer.net/display.php?id=506

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

Expert Comment

by:Naveen Kumar
ID: 34210599
Cyberkiwi

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.

Thanks,
0
 
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.

http://www.oracle-base.com/articles/11g/PivotAndUnpivotOperators_11gR1.php

0
 

Author Closing Comment

by:sachin_dba
ID: 34210687
thnaks.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34210723
@sachin_dba,

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.

Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Suggested Courses

777 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