Rank/Sequence Query Help
791304Aug 13 2010 — edited Aug 24 2010I am working to create a 'Current State' calculation for a thresholding tool, answering the question 'How many months has this been Red/Green?' (1 = Red; 6 = Green)
I have a query that returns the following...
START_TIME MIN_TARGET_LEVEL RTOT Trying to Get
01/01/2010 6 1 1
02/01/2010 6 2 2
03/01/2010 6 3 3
04/01/2010 1 1 1
05/01/2010 6 4 1
06/01/2010 1 2 1
07/01/2010 1 3 2
How do I get the sequence to restart after each change in the Result? I have tried playing with counts; lags; running totals and row num to no avail.
Select START_TIME,
MIN_TARGET_LEVEL,
COUNTS,
SUM(COUNTS) OVER (PARTITION BY MIN_TARGET_LEVEL ORDER by START_TIME) as rtot
From (Select DFL_PROD.nt(START_TIME) START_TIME,
MIN_TARGET_LEVEL,
MIN_TARGET_LEVEL LVL1,
MIN_TARGET_LEVEL LVL2,
1 as COUNTS
from DFL_PROD.WSF_OP_7085_28996_ROLLUP_C
Order By START_TIME) SRC
Order by START_TIME