2

I have got the following data with a column indicating the first record within what we'll call an episode, though there is no episode ID. The ID column indicates and individual person.

ID  StartDate   EndDate     First_Record
1   2013-11-30  2013-12-08  0
1   2013-12-08  2013-12-14  NULL
1   2013-12-14  2013-12-16  NULL
1   2013-12-16  2013-12-24  NULL
2   2001-02-02  2001-02-02  0
2   2001-02-03  2001-02-05  NULL
2   2010-03-11  2010-03-15  0
2   2010-03-15  2010-03-23  NULL
2   2010-03-24  2010-03-26  NULL 

And I am trying to get a column indicating row number (starting with 0) grouped by ID ordered by start date, but the row number needs to reset when the First_Record column is not null, basically. Hence the desired output column Depth.

ID  StartDate   EndDate     First_Record    Depth
1   2013-11-30  2013-12-08  0               0
1   2013-12-08  2013-12-14  NULL            1
1   2013-12-14  2013-12-16  NULL            2
1   2013-12-16  2013-12-24  NULL            3
2   2001-02-02  2001-02-02  0               0
2   2001-02-03  2001-02-05  NULL            1
2   2010-03-11  2010-03-15  0               0
2   2010-03-15  2010-03-23  NULL            1
2   2010-03-24  2010-03-26  NULL            2

I can't seem to think of any solutions although I found a similar thread, but I'm needing help to translate it into what I'm trying to do. It has to use the First_Record column, as it has been set from specific conditions. Any help appreciated

xtna
  • 21
  • 2

2 Answers2

3

If you can have only one episode per person (as in your sample data) you can just use row_number():

select t.*, row_number() over (partition by id order by startDate) - 1 as depth
from t;

Otherwise, you can calculate the episode grouping using a cumulative sum and then use that:

select t.*,
       row_number() over (partition by id, grp order by startDate) - 1 as depth
from (select t.*,
             count(first_record) over (partition by id order by startdate) as grp
      from t
     ) t;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Now the depth will start from 0.

SELECT t.*
    ,convert(INT, (
            row_number() OVER (
                PARTITION BY id ORDER BY startDate
                )
            )) - 1 AS Depth
FROM t;