SELECT dates into Different Columns - A better way? [message #259508] |
Wed, 15 August 2007 15:05 |
hdogg
Messages: 94 Registered: March 2007
|
Member |
|
|
Structure in Monthly Job Revenue
Month Revenue Jobcode
01/31/07 209220 45456
02/28/07 2233221 45664
...ETC
Query A Structure (new table)
Jobcode January February March...etc
45456 5644 54545 544
45457 6654545 45545 54545
As you can see I have been selecting each month and creating a new column for each one. It takes a join for each one and tons of select statements....
Is there a better way to create these columns????
Query A
SELECT a3.jobcode,
january,
february
FROM
(SELECT a1.jobcode,
SUM(january) AS
january,SUM(july) AS
july
FROM
(SELECT mtdrev AS
january,
jobcode
FROM forecast_data
WHERE to_char(to_date(period, 'mm/dd/rr'), 'mm') = '01')
a1,
(SELECT jobcode,
mtdrev AS
july
FROM forecast_data
WHERE to_char(to_date(period, 'mm/dd/rr'), 'mm') = '02')
a2
WHERE a1.jobcode = a2.jobcode
GROUP BY a1.jobcode)
a3
|
|
|
|
Re: SELECT dates into Different Columns - A better way? [message #259516 is a reply to message #259509] |
Wed, 15 August 2007 15:39 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
Oh crap, you beat me to it.
I noted in the 11G docs about the new PIVOT command (thanks for pointing out that the docs were updated).
It's going to be fun to now answer the daily question about pivoting with this kind of response, when the poster does not mention their Oracle version. I know Michel is going to have a field day with this.
But I think your parenthesis are incorrect in your example. I don't have an 11g database to try this out yet.
[Updated on: Wed, 15 August 2007 15:42] Report message to a moderator
|
|
|
|