我有以下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;
我想是這樣的話,不過查詢只顯示因爲查詢本身:( – user2861854
怎麼樣,如果你在WHERE子句中選擇添加這一點,只是得到dtmColumn的MAX結果: 'dtmColumn =(SELECT MAX(dtmColumn)FROM Table WHERE dtmColumn
Tobberoth