简体   繁体   English

如何检查是否有日期重叠?

[英]How to check if there is a date overlap?

Trying to figure out if there is a 60 days (two 30 days supply) overlap for two or more drugs. 试图弄清楚两种或多种药物是否有60天(两个30天的供应量)重叠。 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. 这些药物25605和25700的红色突出显示日期重叠60天。

How do I construct a SQL to figure out if there is at least a 60 days overlap? 如何构造SQL以找出是否至少有60天重叠? Any suggestion would be appreciated. 任何建议,将不胜感激。 Thank you 谢谢 在此处输入图片说明

This type of calculation is cumbersome in SQL Server because the date functions are clunky and there is no least() or greatest() function. 在SQL Server中,这种类型的计算很麻烦,因为date函数很笨重,并且没有minimum least()greatest()函数。

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 . 重叠期的开始和结束是dosdos + days_supply This is just a complex case statement: 这只是一个复杂的case陈述:

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. 否则,重叠的日子将被计算在内。

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. 比较s2.Drug_ID > s1.Drug_ID消除了因药物A药物B和再次药剂B比较药物A的连击DateDiff限制的比较在过去60天相对被检查填充到罢了。

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. 这意味着2015年6月25日药品25700的填充量与药品25605产生了60天的重叠。当然,由于两种填充都在同一天发生,因此您也可以说这是药品25605的填充量造成重叠,但这是没有实际差异的区别,所以没关系。 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). 做出的一个重要假设是,当再次填充相同的药物(ing积)时,将没有剩余的任何剂量。 Of course, you probably have no way of determining that. 当然,您可能无法确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM