If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Impossible Pivot?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-25-12, 05:26
tcbenkhard tcbenkhard is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
Impossible Pivot?

Hello everyone,

(tldr down below)

-- edit: Oracle 11g

I have a problem with pivots ive been trying to figure out for quite a while now so I'm hoping someone here can help me:

Pivots and how I use them:
I want to show for week 01 to week 05 for this year, the profit all the people from sales made for every week seperately in columns:

Code:
select employee, week, profit
from randomtable
where week in ('201101','201102','201103','201104','201105')
This will give me results as follow:
Emp0001 201101 203847
Emp0001 201103 27009
Emp0003 201101 238022


BUT I want it displayed like this:
Employee | Week01 | Week02 | Week03 | Week04 | Week05
data | data | data | data | data | data


To achieve this I just take the previous query and add the pivot statement:

Code:
select * from (
select employee, week, profit
from randomtable
where week in ('201101','201102','201103','201104','201105')
) pivot (max(profit) for week in ('201101','201102','201103','201104','201105'))
MY PROBLEM
Now having pivoted this statement (which was easy) I want to make the weeks dynamic, I want it to show the last 5 weeks (>sysdate-35) and also take this into the pivot. Now I cannot use a subquery or a range in the 'for week in ()' part of the pivot, so how can I solve this?


TLDR:
I want my pivot columns to be dynamic/be created from a subquery/range; how?

Last edited by tcbenkhard; 01-25-12 at 06:00.
Reply With Quote
  #2 (permalink)  
Old 01-25-12, 09:46
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Google is your besr friend.
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 01-25-12, 10:01
tcbenkhard tcbenkhard is offline
Registered User
 
Join Date: Jan 2011
Posts: 8
Quote:
Originally Posted by LKBrwn_DBA View Post
Google is your besr friend.
Tx bro! You're no longer needed on this forum since you just solved all the problems here!


Anyone with a brain and who's not an analphabetic that DOES have a solution?
Reply With Quote
  #4 (permalink)  
Old 01-25-12, 15:38
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Thumbs down

Quote:
Originally Posted by tcbenkhard View Post
Tx bro! You're no longer needed on this forum since you just solved all the problems here!

Anyone with a brain and who's not an analphabetic that DOES have a solution?
Anyone with a brain who is not a moron should know that this question has been asked and answered many times, you are just too lazy search, perhaps even you did not click on the link I posted.

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Last edited by LKBrwn_DBA; 01-25-12 at 16:23.
Reply With Quote
  #5 (permalink)  
Old 01-25-12, 16:43
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,264
whoa
can we keep the personal abuse in check please guys
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 01-26-12, 02:49
flyboy flyboy is offline
Registered User
 
Join Date: Mar 2007
Posts: 546
Quote:
Originally Posted by tcbenkhard View Post
Anyone with a brain and who's not an analphabetic that DOES have a solution?
What about the one which was given you by spacebar in your previous thread about similar topic: The hellhole called 'pivot' ?

In short: a function returning a cursor based on dynamic sql. Of course, your client have to be able to treat such a cursor. Otherwise, it (whatever it is) has to build that dynamic query itself.

Alternatively, you may play with PIVOT XML option, as described e.g. in this article: http://www.oracle-base.com/articles/...tors_11gR1.php
I hope you will be able to use your own brain to transfer the XML output to ordinary columns. Here is a clue: XML Parsing help
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On