0

Trying to figure out if there is a 60 days (two 30 days supply) overlap for two or more drugs. Following table has a person name, day of fill/re-fill, drugid and days of supply.

Red high-lighted dates are overlapped for 60 days supply for these drugids 25605 and 25700.

How do I construct a SQL to figure out if there is at least a 60 days overlap? Any suggestion would be appreciated. Thank you enter image description here

jarlh
  • 42,561
  • 8
  • 45
  • 63
poshan
  • 3,069
  • 5
  • 20
  • 30
  • This would be easier in SQL Server 2012+. Any possibility you can use a more recent version of SQL Server? Also, do the overlaps have to be consecutive? – Gordon Linoff Sep 09 '15 at 18:49
  • Hi Gordon Linoff, I can use sql server 2012 as well. Could you please provide some direction? Thank you – poshan Sep 09 '15 at 18:57
  • Possible duplicate of [Determine whether two date ranges overlap](http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap/328558#328558). At the very least, there is useful information there. The main difference is the '60-day' requirement in this question. – Jonathan Leffler Sep 09 '15 at 19:09

2 Answers2

0

This type of calculation is cumbersome in SQL Server because the date functions are clunky and there is no least() or greatest() function.

But, you can do it. It is essentially a self join with an aggregation. The key idea is determining the days of overlap. The beginning and ending of the overlap period is either the dos or dos + days_supply. This is just a complex case statement:

select t.member, t.drug_id, t2.drug_id,
       sum(datediff(day, (case when t.dos <= t2.dos then t2.dos else t.dos end),
                    (case when dateadd(day, t.days_supply, t.dos) <= dateadd(day, t2.days_supply, t2.dos)
                          then dateadd(day, t.days_supply, t.dos)
                          else dateadd(day, t2.days_supply, t2.dos)
                     end)
           ) as days_overlap
from table t join
     table t2
     on t.member = t2.member and t.drug_id < t2.drug_id and
        t.dos between dateadd(day, -t.days_supply, t2.dos) and
                      dateadd(day, t2.days_supply, t2.dos)
group by t.member, t.drug_id, t2.drug_id;

The code might have an off-by-one error -- overlaps are always a bit confusing, but some real data usually clears things up quickly.

Note: This assumes that the drugs do not overlap with themselves. This is an important assumption; otherwise the days of overlaps will be overcounted.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Hi Gordon, I really appreciate it. Could you please help me to understand what each line of code is doing. I am trying to break it down and understand it piece by piece but am failing to digest it :) To me its a complex piece of SQL code. Thank you – poshan Sep 10 '15 at 14:49
  • Queries on time intervals are usually rather complicated. I would suggest that you generate some sample data and look at the results without the `group by`. This will give you a feel for how the data is being combined. – Gordon Linoff Sep 10 '15 at 22:02
0

An easy way (and you can build this up by parts), is this:

select  s1.Member, s2.DOS, s1.Drug_ID [First Drug], s2.Drug_ID [Second Drug]
from    Stream s1
join    Stream s2
    on  s2.Member   = s1.Member
    and s2.Drug_ID  > s1.Drug_ID
    and DateDiff( Day, s1.DOS, s2.DOS ) between 0 and 60
group by s1.Member, s2.DOS, s1.Drug_ID, s2.Drug_ID
having Sum( s1.Days_Supply ) >= 60;

The comparison s2.Drug_ID > s1.Drug_ID eliminates the double hit caused by comparing drug A with drug B and again drug B with drug A. The DateDiff limits the comparison to fills in the last 60 days relative to the fill being examined.

This produces a single row from the data stream you provided:

Member DOS        First Drug  Second Drug
------ ---------- ----------- -----------
Joe    06/25/2015 25605       25700

Which means that the drug fill of drug 25700 on 06/25/2015 caused a 60 day overlap with drug 25605. Of course, since both fills occurred on the same day, you could just as well say that it was the fill of drug 25605 which caused the overlap, but as that is a distinction without a real difference, it doesn't matter. Check it against real data to make sure it produces relevant results.

One important assumption made is that there will be no doses left over of a drug when that same drug is filled again (hoarding). Of course, you probably have no way of determining that.

TommCatt
  • 5,498
  • 1
  • 13
  • 20
  • Thank you. I will test this too.But how do I get overlap days in here? Could you please update it? I appreciate your suggestion. – poshan Sep 10 '15 at 20:16
  • I'm not sure exactly what you want to see. – TommCatt Sep 10 '15 at 20:21
  • Hi Tom, I would like to find out number of days overlapped if two or more medications are taken during the same period. In this particular example, based on the DOS (days of service), I would like to find out how many days these two medications are overlapped. – poshan Sep 10 '15 at 20:42
  • You specify a 60 day overlap and you look back only 60 days. So the only result possible is exactly 60. You can hard-code that in if you want. – TommCatt Sep 11 '15 at 00:55