根据另一个日期显示最近的日期结果

时间:2013-11-05 07:04:23

标签: sql date

我有以下SQL查询,我发现有多个Consult,SIM,PLAN,TreatmentStart和TreatmentFinish结果。

使用MAX仅显示最新结果,但我需要说,计划显示在TreatmentStart之前最近的最近日期,而SIM则显示计划日期之前最近的最近日期。

开始日期基于最近的TreatmentStart日期。

我只是想知道是否有人可以指出我正确的方向,因为我写了长篇大论的SQL查询,需要2分多钟才能完成......

以下是查询:

SELECT  s.Pat_Name, s.IDA, s.Pat_ID1, 
ShortDesc = MAX(CASE WHEN s.SysDefStatus = 'SC' THEN s.Short_Desc END),
Consult = MAX(CASE WHEN s.Activity = '123'
                    AND s.SysDefStatus = 'C' THEN s.App_DtTm END),
SIM = MAX(CASE WHEN s.Activity = '456'
                      THEN s.App_DtTm END),
PLANNING = MAX(CASE WHEN s.Activity = '789'
                      THEN s.App_DtTm END),
TreatmentStart = MAX(CASE WHEN s.SysDefStatus = 'SC' THEN s.App_DtTm END),
TreatmentFinished = MAX(CASE WHEN s.SysDefStatus = 'FC' THEN s.App_DtTm END)
FROM    vw_Schedule s 
WHERE   s.Activity IN ('123', '456', '789')
OR      s.SysDefStatus IN ('SC', 'FC', 'C')
GROUP BY s.Pat_Name, s.IDA, s.Pat_ID1

)

SELECT  Pat_Name,
IDA,
Tx_Intent,
ShortDesc,
Consult,
Days = (DATEDIFF(dd, Consult, SIM)+1)-(DATEDIFF(wk, Consult, SIM)*2),
SIM,
Days = (DATEDIFF(dd, SIM, PLANNING)+1)-(DATEDIFF(wk, SIM, PLANNING)*2),
PLANNING,
Days = (DATEDIFF(dd, PLANNING, TreatmentStart)+1)-(DATEDIFF(wk, PLANNING, TreatmentStart)*2),
TreatmentStart,
Days = (DATEDIFF(dd, TreatmentStart, TreatmentFinished)+1)-(DATEDIFF(wk, TreatmentStart, TreatmentFinished)*2),
TreatmentFinished
FROM T;

2 个答案:

答案 0 :(得分:0)

如果你选择MAX(dtmColumn)WHERE dtmColumn< dtmColumn2?这应该给你的结果是dtmColumn最接近dtmColumn2,但显然只是前一个日期。

似乎是最简单的方法,但是,我可以想象你的案例过于复杂,无法修复它。

答案 1 :(得分:0)

以下查询正如我所希望的那样工作并显示正确的结果。

WITH PatCPlanRanked AS (
SELECT
Pat_ID1,
Tx_Intent,
Eff_DtTm,
Ranking = ROW_NUMBER() OVER (PARTITION BY Pat_ID1 ORDER BY Eff_DtTm DESC)
FROM PatCPlan
),
T AS (
SELECT s.IDA, s.Pat_ID1, s.Pat_Name, TreatmentStartDate = MAX(CASE WHEN s.SysDefStatus = 'SC' THEN s.App_DtTm END),
MAX(t.App_DtTm) AS PlanDate, MAX(u.App_DtTm) AS SIMDate, MAX(c.App_DtTm) AS ConsultDate,
ShortDesc = MAX(CASE WHEN s.SysDefStatus = 'SC' THEN s.Short_Desc END), p.Tx_Intent,
Location = MAX(CASE WHEN s.SysDefStatus = 'SC' THEN s.Location END)
FROM vw_Schedule s
LEFT OUTER JOIN vw_Schedule t
ON s.Pat_ID1 = t.Pat_ID1
LEFT OUTER JOIN vw_Schedule u
ON s.Pat_ID1 = u.Pat_ID1
LEFT OUTER JOIN vw_Schedule c
ON s.Pat_ID1 = c.Pat_ID1
LEFT OUTER JOIN PatCPlanRanked p
ON s.Pat_ID1=p.Pat_ID1 AND p.Ranking = 1

WHERE s.SysDefStatus IN ('SC') AND t.App_DtTm < s.App_DtTm AND u.App_DtTm < t.App_DtTm AND c.App_DtTm < u.App_DtTm 
AND t.Activity IN ('123')
AND u.Activity IN ('456') 
AND c.Activity IN ('789') 
AND c.SysDefStatus IN (' C') 
GROUP BY s.IDA, s.Pat_ID1, s.Pat_Name, p.Tx_Intent
)

SELECT  IDA, 
    Pat_Name,
    ShortDesc,
    Tx_Intent,
    Location,
    ConsultDate,
    Days = (DATEDIFF(dd, ConsultDate, SimDate))-(DATEDIFF(wk, ConsultDate, SimDate)*2),
    SimDate,
    Days = (DATEDIFF(dd, SimDate, PlanDate))-(DATEDIFF(wk, SimDate, PlanDate)*2),
    PlanDate,
    Days = (DATEDIFF(dd, PlanDate, TreatmentStartDate))-(DATEDIFF(wk, PlanDate, TreatmentStartDate)*2),
    TreatmentStartDate
FROM T

然而,查询大约需要35秒才能完成,任何人都可以就如何优化此问题向我提出任何建议吗?

欢呼人们