0

我有以下 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;
4

2 回答 2

0

如果您选择 MAX(dtmColumn) WHERE dtmColumn < dtmColumn2,您不会得到最近的日期吗?这应该为您提供 dtmColumn 最接近 dtmColumn2 的结果,但显然只有前一个日期。

似乎是最简单的方法,但是,我可以想象您的情况太复杂,无法解决这么简单的问题。

于 2013-11-05T07:23:45.810 回答
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 秒才能完成,谁能给我任何关于如何优化它的建议?

欢呼伙计们

于 2013-11-06T09:15:23.280 回答