1

I've got a SQL table where I want to find the first and last dates of a group of records, providing they're sequential.

 Patient | TestType | Result |     Date
------------------------------------------
    1    |    1     |   A    |  2012-03-04
    1    |    1     |   A    |  2012-08-19
    1    |    1     |   B    |  2013-05-27
    1    |    1     |   A    |  2013-06-20
    1    |    2     |   X    |  2012-08-19
    1    |    2     |   X    |  2013-06-20
    2    |    1     |   B    |  2014-09-09
    2    |    1     |   B    |  2015-04-19

Should be returned as

Patient | TestType | Result | StartDate   |   EndDate
--------------------------------------------------------
   1    |    1     |   A    |  2012-03-04 | 2012-08-19 
   1    |    1     |   B    |  2013-05-27 | 2013-05-27
   1    |    1     |   A    |  2013-06-20 | 2013-06-20 
   1    |    2     |   X    |  2012-08-19 | 2013-06-20
   2    |    1     |   B    |  2014-09-09 | 2015-04-19

The problem is that if I just group by Patient, TestType, and Result, then the first and third rows in the example above would become a single row.

Patient | TestType | Result | StartDate   |   EndDate
--------------------------------------------------------
   1    |    1     |   A    |  2012-03-04 | 2013-06-20 
   1    |    1     |   B    |  2013-05-27 | 2013-05-27
   1    |    2     |   X    |  2012-08-19 | 2013-06-20
   2    |    1     |   B    |  2014-09-09 | 2015-04-19

I feel like there's got to be something clever I can do with a partition, but I can't quite figure out what it is.

5
  • 1
    What determines the sequence or order of your records? Rows by themselves don't have any order. You need a column that defines the sequence.
    – sstan
    Aug 20, 2015 at 18:39
  • Ordered by date, within the patient/type group.
    – Batman
    Aug 20, 2015 at 18:41
  • I think you misunderstood me. In your sample data, you presented 8 rows. It would seem that the order of those 8 rows is significant to determine how to calculate the results. How are those 8 rows ordered?
    – sstan
    Aug 20, 2015 at 18:43
  • There's no implicit order. I just picked that order because I thought it would make the problem clear.
    – Batman
    Aug 20, 2015 at 18:45
  • Well, if there is no implicit order, then there is no guarantee that you'll get consistent results either regardless of how you choose to solve this problem. A consistent and reproducible solution to your problem depends completely on the order of that initial data set.
    – sstan
    Aug 20, 2015 at 18:50

3 Answers 3

2

There are several ways to approach this. I like identifying the groups using the difference of row number values:

select patient, testtype, result,
       min(date) as startdate, max(date) as enddate
from (select t.*, 
             (row_number() over (partition by patient, testtype order by date) -
              row_number() over (partition by patient, testtype, result order by date)
             ) as grp
      from table t
     ) t
group by patient, testtype, result, grp
order by patient, startdate;
0
select patient, testtype, result, date as startdate, 
isnull(lead(date) over(partition by patient, testtype, result order by date), date) as enddate
from tablename;

You can use lead function to get the value of date (as enddate) from the next row in each group.

SQL Fiddle with sample data.

1
  • Your fiddle show extra rows. According to OP the result should group first two A because are together, then a single B, then a single A, then group both X and both B because are consecutive. Aug 20, 2015 at 19:53
0

See if this gives you what you need.

with T1 as (
  select
    *,
    case when lag(Patient,1)
           over (order by Patient, TestType, Result) = Patient
          and lag(TestType,1) 
           over (order by Patient, TestType, Result) = TestType
          and lag(Result,1)
           over (order by Patient, TestType, Result) = Result
    then null else 1 end as Changes
  from t
), T2 as (
  select
    Patient,
    TestType,
    Result,
    dt,
    sum(Changes) over (
      order by Patient, TestType, Result, dt
    ) as seq
  from T1
)
  select
    Patient,
    TestType,
    Result,
    min(dt) as dtFrom,
    max(dt) as dtTo
  from T2
  group by Patient, TestType, Result, seq
  order by Patient, TestType, Result
1
  • Note that my query and Gordon’s give different results if you add this row to your sample data: (1,1,'B','2013-05-27'). I’m not sure what you want as a result in that case.
    – Steve Kass
    Aug 21, 2015 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.