【问题标题】:sql join 2 tables on earliest date after date in first tablesql在第一个表中的日期之后的最早日期加入2个表
【发布时间】:2015-07-24 01:57:23
【问题描述】:

我的请求与此处描述的类似:sql server - join 2 tables based on earliest date in 2nd table

一个区别是,我需要在第一个表格中找到出院日期之后的最早可用门诊就诊日期,并且该日期在出院日期后的 30 天内。如果在时间范围内不存在后续访问,我想返回 Null。

表 1 'Discharges' - 包含列:ClientId、DischargeFrom、Discharge Date

ClientId    DischargeFrom   DischargeDate
1   Unit A  2009/11/08
1   Unit A  2010/01/05
2   Unit A  2010/01/08
3   Unit B  2010/10/01
4   Unit A  2010/02/04
4   Unit B  2010/04/05
5   Unit A  2010/01/04

表 2 'OutpatientVisits - 包含列:ClientId、FollowUpClinicName、FollowUpVisitDt

ClientId    FollowUpClinicName  FollowUpVisitDt
1   Outpatient_Clinic_1 2009/05/04
1   Outpatient_Clinic_1 2009/07/07
1   Outpatient_Clinic_1 2010/01/14
1   Outpatient_Clinic_1 2010/01/18
2   Outpatient_Clinic_2 2007/11/05
2   Outpatient_Clinic_3 2009/12/22
2   Outpatient_Clinic_1 2010/01/04
5   Outpatient_Clinic_2 2010/01/01
5   Outpatient_Clinic_1 2010/01/11
7   Outpatient_Clinic_3 2010/01/25

一个 ClientId 可以有多个 Discharges(此处为 ClientIds 1 和 4),一个 ClientId 也可以与多个后续访问相关联(ClientIds 1、2 和 5)。我想在出院日期或出院日期之后但在出院日期后 30 天内返回第一个 FollowUpVisitDt。我还想退回那些没有后续访问的出院记录。

我已经尝试了以下语法,但我似乎无法返回最早的访问记录(请参阅 ClientId 1 在同一出院日期有 2 个重复结果)。 ClientId 2 也已出院,但未在结果中列出。

SELECT DISTINCT Discharges.ClientId,
    Discharges.DischargeFrom, 
    Discharges.DischargeDate,
    FollowUpVisits.FollowUpVisitDate,
    DateDiff(DAY,Discharges.DischargeDate,FollowUpVisits.FollowUpVisitDate) As DaysBetween,
    FollowUpVisits.rn
 FROM Discharges LEFT JOIN
 (
 SELECT *, ROW_NUMBER() OVER (PARTITION By ClientId, FollowUpClinicName ORDER BY FollowUpVisitDate ASC) as rn
    FROM [MH].[dbo].OutpatientVisits
 ) As FollowUpVisits
 ON Discharges.ClientId=FollowUpVisits.ClientId
 WHERE (FollowUpVisits.FollowUpVisitDate>=Discharges.DischargeDate and FollowUpVisitDate<=DATEADD(day,30,DischargeDate)) OR FollowUpVisits.FollowUpVisitDate Is Null

结果:

ClientId    DischargeFrom   DischargeDate   FollowUpVisitDate   DaysBetween rn
1   Unit A  2009-11-08  2009-12-27  49  3
1   Unit A  2009-11-08  2010-01-18  71  4
1   Unit A  2010-01-05  2010-01-18  13  4
3   Unit B  2010-10-01  NULL    NULL    NULL
4   Unit A  2010-02-04  NULL    NULL    NULL
4   Unit B  2010-04-05  NULL    NULL    NULL
5   Unit A  2010-01-04  2010-01-11  7   1

非常感谢您的帮助!我正在使用 MSSQL 2005。

【问题讨论】:

    标签: sql-server date join


    【解决方案1】:

    尝试为此使用outer apply

    select d.*, fv.*,
           datediff(day, d.DischargeDt, ov.FollowupVisitDt) as DaysBetween
    from Discharges d outer apply
         (select top 1 ov.*
          from [MH].[dbo].OutpatientVisits ov
          where ov.ClientId = d.ClientId and
                ov.FollowupVisitDt > d.DischargeDt
          order by FollowupVisitDt
         ) fv;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多