通过MRN和date1> = MAX(date2)

时间:2019-01-17 15:55:53

标签: sql-server sql-server-2008

我需要合并两个医院活动报告中的数据。这是怎么回事:患者被送进脊柱科。然后,其中一些人被告知要通风。不久后患者出院。以后,同一位患者可能会或可能不会被转介回脊柱科,并且可能会或可能不会被转介给通气。在两个报告中向我发送了活动数据:

每月活动报告:

[MRN] [NHS Number] [Admission Date] [DoB] [Blah] [Blah]

通风报告

[MRN] [Admission Date]  [Ventilation Days]   [Ventilation Type] [blah] [blah]

通风报告中的准入日期是指他们被要求通风的日期。这可能是同一天,也可能是他们被称为脊椎部的某个日期。

我需要实现的是:在让患者进行通气之前,将每一行连接到最接近的入口。我需要避免重复行,但是我不能将其加入“每月活动报告”中的最新行,因为这很可能是后续引用,而其他信息将不适用。

按照关于Stackoverflow的类似问题的答案,我想到了以下代码:

SELECT [Year], [Month], MRN, [NHS Number], [Admission Date] AS [VD 
Admission Date], 
[Admit date] AS [MAR Admit Date], Days,
[Ventilation Type], [Ventilation Route], [Ventilation Time], [Package of 
care class],
[Para/Tetra/No deficit], [Social charge date commenced ] AS [Social charge 
date], [Discharge date]
FROM Spinal_Costing.Vented_Days VD
LEFT JOIN (SELECT *, ROW_NUMBER() OVER(PARTITION BY [Patient MRN] ORDER BY 
[Admit Date] DESC) AS row
    FROM Spinal_Costing.MAR
 ) MAR ON VD.MRN = MAR.[Patient MRN]
 WHERE MAR.row = 1;

但这将为每个患者返回MAR的最新条目。

2 个答案:

答案 0 :(得分:0)

您走在正确的轨道上,您只需要向该派生表中添加JOIN,即可将Spinal_Costing.MAR表中的行限制为原来的行或出行前的行。

SELECT 
    [Year], 
    [Month], 
    MRN, 
    [NHS Number], 
    [Admission Date] AS [VD Admission Date], 
    [Admit date] AS [MAR Admit Date], 
    Days,
    [Ventilation Type], 
    [Ventilation Route], 
    [Ventilation Time], 
    [Package of care class],
    [Para/Tetra/No deficit], 
    [Social charge date commenced ] AS [Social charge date], 
    [Discharge date]
FROM 
    Spinal_Costing.Vented_Days VD
LEFT JOIN 
    (SELECT 
        *, 
        ROW_NUMBER() OVER(PARTITION BY [Patient MRN] ORDER BY [Admit Date] DESC) AS row
    FROM Spinal_Costing.MAR 
    --added the JOIN and WHERE clause here
    INNER JOIN Spinal_Costing.Vented_Days 
        ON Spinal_Costing.Vented_Days.MRN = Spinal_Costing.MAR.[Patient MRN]
    WHERE  Spinal_Costing.MAR.[Admit Date]  <= Spinal_Costing.Vented_Days.[Discharge date]
 ) MAR ON VD.MRN = MAR.[Patient MRN]
 WHERE MAR.row = 1;

答案 1 :(得分:0)

这也可以通过使用apply来引用Vented_Days中的值,并为每一行简单地返回一个top 1来实现。 cross apply将不会返回null个值,而outer apply将返回:

declare @vd table(MRN int,AdmissionDate date);
declare @mar table(MRN int,AdmissionDate date);

insert into @vd values
 (1,'20190102')
,(1,'20190106')
,(2,'20190104')
,(3,'20190101');

insert into @mar values
 (1,'20190101')
,(1,'20190105')
,(2,'20190102');

select v.MRN
        ,v.AdmissionDate
        ,m.AdmissionDate
from @vd as v
    outer apply (select top 1 m.AdmissionDate
                 from @mar as m
                 where v.MRN = m.MRN
                    and v.AdmissionDate >= m.AdmissionDate
                 order by m.AdmissionDate desc
                ) as m
order by v.MRN
        ,v.AdmissionDate;

输出

+-----+---------------+---------------+
| MRN | AdmissionDate | AdmissionDate |
+-----+---------------+---------------+
|   1 | 2019-01-02    | 2019-01-01    |
|   1 | 2019-01-06    | 2019-01-05    |
|   2 | 2019-01-04    | 2019-01-02    |
|   3 | 2019-01-01    | NULL          |
+-----+---------------+---------------+