Click to See Complete Forum and Search --> : Oracle SQL


coping
06-22-2007, 03:23 PM
I'm using TOAD for Oracle version 8.6.1.0.

I've written this query to pivot the data in a view after the intital load.
I only want to extract the three latest end_dt for each record. Ho do I do that dynamically?

Thanks


CREATE VIEW MYVIEW
AS
select EMP_NBR
, max (case when P_YEAR = CURR_DT then END_DT end) as CURR_YEAR
, max (case when P_YEAR = CURR_DT then X end) as CURR_RATING_X
, max (case when P_YEAR = CURR_DT then Y end) as CURR_RATING_Y
, max (case when P_YEAR = CURR_DT then Z end) as CURR_RATING_Z
, max (case when P_YEAR = CURR_DT-1 then END_DT end) as PREV_YEAR
, max (case when P_YEAR = CURR_DT-1 then X end) as PREV_RATING_X
, max (case when P_YEAR = CURR_DT-1 then Y end) as PREV_RATING_Y
, max (case when P_YEAR = CURR_DT-1 then Z end) as PREV_RATING_Z
, max (case when P_YEAR = CURR_DT-2 then END_DT end) as PREV2_YEAR
, max (case when P_YEAR = CURR_DT-2 then X end) as PREV2_RATING_X
, max (case when P_YEAR = CURR_DT-2 then Y end) as PREV2_RATING_Y
, max (case when P_YEAR = CURR_DT-2 then Z end) as PREV2_RATING_Z
from MYTABLE
where P_YEAR <= CURR_DT
group by EMP_NBR