user3666224
user3666224

Reputation: 71

Calculating between dates fields from a previous record

  Patient Code   Unique Visit Code    AdmitDate     Discharge Date
  91260          10146440             7/20/2013     9/16/2013
  91260          10217043             9/21/2013     11/2/2013

This is a single patient with 2 different visits to the physician, I need to take the Discharge date of the earliest visit (9/16/2013) and the AdmitDate of the most recent visit (9/21/2013) and determine if it's within a 30 day period. How do I accomplish this in SQL 2008

Upvotes: 1

Views: 49

Answers (1)

Nizam
Nizam

Reputation: 4699

Try

SELECT PATIENT_CODE, 
     CASE WHEN DATEDIFF(DAY, MIN(DISCHARGE_DATE), MAX(ADMIT_DATE)) < 30 THEN 1 ELSE 0 END
FROM TABLE1
GROUP BY PATIENT_CODE

The code

CASE WHEN DATEDIFF(DAY, MIN(DISCHARGE_DATE), MAX(ADMIT_DATE)) < 30 THEN 1 ELSE 0 END

will be 1 for those visits which occurs in period less than 30 days.

SQL Fiddle

Upvotes: 1

Related Questions