Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

analytical function - problem with LAG's / LEAD's offset

dusooJun 2 2008 — edited Jun 5 2008

Hi guys,
i have really big select made of 7 inner selects. (ORA 9)
In each step im using analytical functions to get needed values for outer select.
But in the last select, something's wrong with lag / lead function and their offset value.
The problem occurs ONLY when previous calculated COUNTER (counter9 in the example) is NULL.
So if i have "SELECT lag(counter10...), lag(counter9...), lag(counter8...) from ..." and for example counter 8 is null,
the problem with OFFSET apply to counter9 and counter10, which are in front of him.

IF i create temporary table from SELECT NO_5, and run the last part on it, i have no problem at all.

In case you have some free time and want to try it, i can upload sql script for you including DML and DDL.

thanks for any ideas

example of a DATA returned by select NO_5 - counter9 is NULL (also it's prev/next_rn)

DATETIME	MID	RNUM	COUNTER10	PREV_RN_COUNTER10	NEXT_RN_COUNTER10	COUNTER9	PREV_RN_COUNTER9	NEXT_RN_COUNTER9
26.5.2008 10:30	551088	15	2128	14	16			
26.5.2008 10:45	551088	16	2094	15	20			
26.5.2008 11:00	551088	17		16	20			
26.5.2008 11:15	551088	18		16	20			
26.5.2008 11:30	551088	19		16	20			
26.5.2008 11:45	551088	20	2086	16	21			
26.5.2008 12:00	551088	21	2066	20	22			

example of a SELECT.

-- start of select NO_6
SELECT datetime,
       mid,
       prev_rn_counter10, -- RETURNS same values as in NO_5 select
       nvl(counter10, lag(counter10, rnum-prev_rn_counter10) over (partition by mid order by datetime)) counter10_fin,
       nvl(counter9,  lag(counter9,  rnum-prev_rn_counter9) over (partition by mid order by datetime)) counter9_fin
FROM (
-- start of select NO_5
  SELECT datetime,
         mid,
         RNUM,
         counter10,
         max(nvl2(counter10,rnum,null)) over (partition by mid order by rnum rows between unbounded preceding and 1 preceding) prev_rn_counter10,
         min(nvl2(counter10,rnum,null)) over (partition by mid order by rnum rows between 1 following and unbounded following) next_rn_counter10,
         counter9,
         max(nvl2(counter9,rnum,null)) over (partition by mid order by rnum rows between unbounded preceding and 1 preceding) prev_rn_counter9,
         min(nvl2(counter9,rnum,null)) over (partition by mid order by rnum rows between 1 following and unbounded following) next_rn_counter9
  FROM (
   SELECT ...
   FROM ...
   ...
   --NEXT 5 inner SELECTs are in here...
  )
)

SELECT NO_6 returns exactly the same data in counter10 as returned in NO_5 select - what's wrong.
IF i put NVL ( ..., 3) in the lag's offset, i get the data, what means that prev_rn_counter10 is NULL for LAG function,
but in real, it's not null, because i can retrieve the data from it.

SELECT datetime,
       mid,
       nvl(counter10, lag(counter10, NVL(rnum-prev_rn_counter10, 3)) over (partition by mid order by datetime)) counter10_fin,
...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 3 2008
Added on Jun 2 2008
12 comments
2,042 views