Solved

Oracle Pivot Query

Posted on 2010-11-24
9
737 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 500 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to recover a database from a user managed backup

688 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