I am trying to apply ROW_NUMBER()to increase the counter based on specific conditions.
My data looks like this: goal counter - column Prep
id DSR PrepIndicator Prep
--------------------------------------
1662835 -1 1 1
1662835 14 2 2
1662835 14 2 3
1662835 20 2 4
1667321 -1 1 1
1667321 30 2 2
1667321 14 2 3
1680648 -1 1 1
1680648 14 2 2
1680648 60 1 1
1680648 14 2 2
1680648 14 2 3
1683870 -1 1 1
1683870 12 2 2
1683870 10 2 3
1683870 60 1 1
1683870 7 2 2
Ignoring the column PrepIndicatorfor now, the business logic I'm trying to implement is as follows:
- For each of the Ids, starting at 1, increment the counter
Prepif the DSR is less than 42. - If it is 42 or more, reset the Prep counter to 1.
PrepIndicator, in fact, creates a flag to implement this, if PrepIndicator = 1, then Prep = 1. If PrepIndicator = 2, then increment Prep.
I would rather achieve this without a column PrepIndicator, if possible.
ROW_NUMBER()?
ROW_NUMBER() OVER (PARTITION BY id, PrepIndicator ORDER BY id)
, DSR >= 42.
. !