Solved

Oracle Pivot Queries

Posted on 2004-10-15
4
1,205 Views
Last Modified: 2007-12-19
Hey there experts.

Say for example I have a query that returns a result set like:

Date          Job           Pay          Bonus        Tax
------------------------------------------------------------
07/2004    Manager    80000       5000        32000
07/2004    Clerk         20000        200          1000
08/2004    Clerk         25000        850          1300
08/2004    Cleaner     12000         0             500


What I would like to achieve is to perform a pivot on this result set so that the query returns the figures in the following way:


Job            Pay (July)       Bonus (July)       Tax (July)      Pay (August)       Bonus (August)       Tax (August)    
---------------------------------------------------------------------------------------------------------------------------------
Manager     80000              5000                    32000             0                         0                          0
Clerk          20000              200                       1000           25000                  850                      1300
Cleaner          0                    0                           0              12000                   0                          500


I hope that someone can help me out with this one!!

If you need some scripts to vreate and populate a test table then let me know!!!

Thanks
0
Comment
Question by:runcsmeduncs
4 Comments
 
LVL 15

Accepted Solution

by:
andrewst earned 100 total points
ID: 12319437
It will look something like this:

select job
,      sum(DECODE(to_char(date,'Month'),'July', pay)) as "sum_July_pay"
,      sum(DECODE(to_char(date,'Month'),'July', bonus)) as "sum_July_bonus"
,      sum(DECODE(to_char(date,'Month'),'July', tax)) as "sum_July_tax"
,      sum(DECODE(to_char(date,'Month'),'August', pay)) as "sum_August_pay"
,      sum(DECODE(to_char(date,'Month'),'August', bonus)) as "sum_August_bonus"
,      sum(DECODE(to_char(date,'Month'),'August', tax)) as "sum_August_tax"
from   mytable
group by job
order by job
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

840 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