I am using SQL Server 2012
I have data that looks like this ORDER BY ID, LabDate DESC
I need to move back through time looking for the next row where LabResult < 8. If found I need to retrieve the current row values for ID, LabDate and LabResult. If the there is only one value then or there are no values then retrieve the earliest or only value
For the above data the resultant set would be 2 rows
I read about LAG and LEAD and self-join to try to solve this problem, but I cannot retrieve just one row per ID. I added a ROW_NUMBER column, but not sure what to do with it.
I really don't know where to start so, any help would be appreciated.
Thank you.
You don't know what you don't know...
I have data that looks like this ORDER BY ID, LabDate DESC
Code:
ID LabDate LabResult
1 2012-05-18 11.2
1 2011-02-03 8.3
1 2010-10-01 7.5
1 2009-03-04 8.6
2 2013-06-12 10.5
2 2011-02-03 7.5
2 2009-03-04 8.9
3 2014-01-31 9.5
3 2009-03-04 8.9
4 2008-11-15 9.8
For the above data the resultant set would be 2 rows
Code:
ID LabDate LabResult
1 2011-02-03 8.3
2 2013-06-12 10.5
3 2009-03-04 8.9
4 2008-11-15 9.8
I read about LAG and LEAD and self-join to try to solve this problem, but I cannot retrieve just one row per ID. I added a ROW_NUMBER column, but not sure what to do with it.
I really don't know where to start so, any help would be appreciated.
Thank you.
You don't know what you don't know...