Solved

Oracle Pivot Queries

Posted on 2004-10-15
4
1,202 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.

Join & Write a Comment

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 …
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now