I have a bunch of patient prescriptions, each having a certain start date and an end date. I would like to find the instances where a patient has been taking more than one drug (at the same time) in the same drug category for more than 2 days. duration should overlap.
Table structure looks like this:
PatientID StartDate EndDate Drug DrugCategory
001 1/1/2013 1/5/2013 A Cat1
001 1/1/2013 1/4/2013 B Cat1
001 1/10/2013 1/12/2013 C Cat1
002 ....... ........ ............. .........
In the sample code above, Patient 001 was prescribed 3 drugs in the same category and the first two drug overlapped in duration more than 2 days. So, I would like the query return the first two records.
Hope someone can help. This is using SQL Server 2008 R2 btw.