SQL Server Read next record?

f0ofjuux  于 2023-06-04  发布在  其他
关注(0)|答案(1)|浏览(162)

We are on MS SQL-Server 2012.Users want to know if the time of the patients next admission date is less than 30 days for any reason. Doesn’t have to be seen by same provider

I am not sure how to read the next record, if the MRN ID is the same, then calculate the difference in days between the record you are on and the next next record.

For example:

Record 1 : MRNID =33 Discharge date = 1/1/2016  
Record 2 : MRNID = 33 Admission date = 2/2/2016

MRNIDs are the same, so I calculate. Then I compare record 2 to record 3 and do the same process.

h6my8fg2

h6my8fg21#

Use Lead() window function

select mrnid, 
       admission_date, 
       discharge_date, 
       lead(admission_date) over (partition by mrnid order by admission_date) next_date
  from table;

SAMPLE OUTPUT

mnrid   admission_date  lead(admission_date)
33  2016-01-01  2016-01-02
33  2016-01-02  2016-01-03
33  2016-01-03  2016-01-04
33  2016-01-04  null
34  2016-01-01  2016-01-02
34  2016-01-02  2016-01-03
34  2016-01-03  2016-01-04
34  2016-01-04  null

相关问题