以下 sql server 2005 查询当前正在成功运行。我需要修改“p.Tx_Intent”以根据同一表中的日期列“p.Eff_DtTm”显示,因此它只显示患者的最新 Tx_Intent(Eff_DtTm)。
WITH T AS
( SELECT s.Pat_Name, s.IDA, s.Pat_ID1, p.Tx_Intent,
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
LEFT OUTER JOIN PatCPlan p
ON s.Pat_ID1=p.Pat_ID1
WHERE s.Activity IN ('123', '456', '789')
OR s.SysDefStatus IN ('SC', 'FC', 'C')
GROUP BY s.Pat_Name, s.IDA, s.Pat_ID1, p.Tx_Intent
)
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;
好的,所以我想出了一种方法来确定最近的日期,我只是在将它与我当前的查询结合起来时遇到问题。这是我需要与顶级查询相结合的查询
SELECT Pat_ID1, Tx_Intent from (select
Pat_ID1
,Tx_Intent
,Eff_DtTm
,row_number() over (partition by Pat_ID1 order by abs(datediff(dd, Eff_DtTm, getdate()))) Ranking
from PatCPlan) xx
where Ranking = 1
谢谢你的帮助!