2013-11-05 144 views
0

我有以下SQL查詢,我發現有多個Consult,SIM,PLAN,TreatmentStart和TreatmentFinish結果。顯示最近的日期結果基於另一個日期

使用MAX只顯示最新結果,但我需要說,計劃顯示TreatmentStart之前最近的最近日期,以及SIM顯示計劃日期之前最近的最近日期。

開始日期基於最新的TreatmentStart日期。

我只是想知道如果任何人都可以點我在正確的方向,因爲我寫了長篇大論了佔用更多,然後2分鐘就可以完成SQL查詢...

這裏有疑問:

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; 

回答

0

如果您使用MAX(dtmColumn)WHERE dtmColumn < dtmColumn2,您不會得到最近的最近日期嗎?這應該給你最接近dtmColumn2的dtmColumn的結果,但顯然只有前一個日期。

似乎是最簡單的方式來做到這一點,但是,我可以想象你的情況太複雜了,以至於很難解決這個問題。

+0

我想是這樣的話,不過查詢只顯示因爲查詢本身:( – user2861854

+0

怎麼樣,如果你在WHERE子句中選擇添加這一點,只是得到dtmColumn的MAX結果: 'dtmColumn =(SELECT MAX(dtmColumn)FROM Table WHERE dtmColumn Tobberoth

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秒才能完成誰能給我如何我可以優化這有什麼建議?

歡呼傢伙