0

我有一堆病人处方,每个都有特定的开始日期和结束日期。我想找出患者服用同一药物类别中的一种以上药物超过 2 天的情况。持续时间应该重叠。

表结构如下所示:

PatientID  StartDate  EndDate    Drug        DrugCategory
1          1/1/2013   1/5/2013   A           Cat1
1          1/1/2013   1/4/2013   B           Cat1
1          1/10/2013  1/12/2013  C           Cat1
2     .......    ........   .............  .........

如上所示,Patient-1 开了 3 种相同类别的药物,前两种药物的持续时间重叠超过 2 天。所以,对于这个例子,我希望查询返回 Patient-1 的前两条记录以及药物名称,patientid。

希望有人可以提供帮助。这是使用 SQL Server 2008 R2 顺便说一句。

4

3 回答 3

0

Please test this thoroughly before using. I have tested it and so far it looks good but not done an exhaustive testing. Would be great if you can test it out further sufficient to your requirements and can point any anomalies in the result if present or take it forward and modify if required.

--Test data:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Prescriptions](
    [PatientID] [int] NULL,
    [StartDate] [datetime] NULL,
    [EndDate] [datetime] NULL,
    [Drug] [varchar](50) NULL,
    [DrugCategory] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A13A00000000 AS DateTime), CAST(0x0000A13E00000000 AS DateTime), N'D', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A13A00000000 AS DateTime), CAST(0x0000A13B00000000 AS DateTime), N'E', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A13800000000 AS DateTime), CAST(0x0000A13B00000000 AS DateTime), N'F', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A13800000000 AS DateTime), CAST(0x0000A13900000000 AS DateTime), N'G', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A12300000000 AS DateTime), CAST(0x0000A13900000000 AS DateTime), N'Z', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A12300000000 AS DateTime), CAST(0x0000A13A00000000 AS DateTime), N'Y', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A13900000000 AS DateTime), CAST(0x0000A13D00000000 AS DateTime), N'A', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A13900000000 AS DateTime), CAST(0x0000A13C00000000 AS DateTime), N'B', N'Cat1')
INSERT [dbo].[Prescriptions] ([PatientID], [StartDate], [EndDate], [Drug], [DrugCategory]) VALUES (1, CAST(0x0000A14200000000 AS DateTime), CAST(0x0000A14400000000 AS DateTime), N'C', N'Cat1')

Query Used:

SELECT DISTINCT PatientID,StartDate,EndDate,Drug,DrugCategory FROM (
SELECT 
    DATEDIFF(dd,a.startdate,b.startdate) c1
    ,DATEDIFF(dd,a.enddate,b.enddate)c2
    ,DATEDIFF(dd,a.startdate,b.enddate) c3
    ,DATEDIFF(dd,a.enddate,b.startdate) c4
    ,DATEDIFF(dd,a.startdate,b.enddate)+DATEDIFF(dd,a.enddate,b.startdate) c34
    ,a.PatientID
    ,a.StartDate
    ,a.EndDate
    ,a.Drug 
    ,a.DrugCategory 
    ,b.PatientID  AS PatientID1
    ,b.StartDate  AS StartDate1
    ,b.EndDate   AS EndDate1
    ,b.Drug   AS Drug1
    ,b.DrugCategory  DrugCategory1
FROM Prescriptions a
,Prescriptions b
WHERE a.patientid=b.patientid
AND a.DrugCategory= b.DrugCategory
and a.drug<>b.drug
)a 
WHERE c1*c2*c3*c4 <0
AND c3>2
and c4<=-2
ORDER BY 1,2,3,4

Results:

PatientID   StartDate               EndDate                 Drug                                               DrugCategory
----------- ----------------------- ----------------------- -------------------------------------------------- --------------------------------------------------
1           2012-12-10 00:00:00.000 2013-01-02 00:00:00.000 Y                                                  Cat1
1           2012-12-31 00:00:00.000 2013-01-03 00:00:00.000 F                                                  Cat1
1           2013-01-01 00:00:00.000 2013-01-04 00:00:00.000 B                                                  Cat1
1           2013-01-01 00:00:00.000 2013-01-05 00:00:00.000 A                                                  Cat1
1           2013-01-02 00:00:00.000 2013-01-06 00:00:00.000 D                                                  Cat1

(5 row(s) affected)
于 2013-05-28T18:49:05.303 回答
0

您应该能够将处方表加入到患者ID 和drugCategory 字段中,其中drugName 不同,并且第二个实例的startDate 或endDate 跨越第一个实例的startDate/endDate。然后,通过减去 max(startDates) 和 min(endDates) 之间的天数来确定重叠范围。如果重叠大于 2 天,则返回该行:

select *, datediff(d, start_max, end_min) as overlap
from (
    SELECT 
      P.PatientID, P.StartDate, P.EndDate, P.Drug, P.DrugCategory, 
      P1.StartDate AS p1_start, P1.EndDate AS p1_end, P1.Drug AS p1_drug, 
      CASE WHEN p.startdate >= P1.startdate THEN p.startdate ELSE P1.startdate END AS start_max, 
      CASE WHEN p.EndDate <= P1.EndDate THEN p.EndDate ELSE P1.EndDate END AS end_min
    FROM  
      dbo.Prescriptions p INNER JOIN
      dbo.Prescriptions AS P1 ON 
      P.PatientID = P1.PatientID AND 
      P.DrugCategory = P1.DrugCategory AND 
      P.Drug <> P1.Drug
    WHERE 
      (P1.StartDate >= P.StartDate AND P1.StartDate <= P.EndDate) OR
      (P1.EndDate >= P.StartDate AND P1.EndDate <= P.EndDate)
) t
where
datediff(d, start_max, end_min) > 2
于 2013-05-28T21:28:47.127 回答
0

您希望它们作为单独的行还是作为一行?如果您希望它们作为单独的行,这应该可以;否则你可以旋转结果。

create table want as
    select H.* from have H, have V
        where H.drug ne V.drug
            and H.PatientID=V.PatientID
            and H.startDate <= V.startDate
            and V.startDate <= H.endDate-2
    union select V.* from have H, have V
        where H.drug ne V.drug
            and H.PatientID=V.PatientID
            and H.startDate <= V.startDate
            and V.startDate <= H.endDate-2
            ;

我合并了 H 和 V 记录,我确信有一种更有效的方法可以做到这一点,但不容易想出一个。(仅 H 适用于给出的示例,但对于开始日期并不总是相等的更合适的示例,您还需要 V 行。)

于 2013-05-28T16:47:56.933 回答