0

以下 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

谢谢你的帮助!

4

1 回答 1

0

不完全确定,但这里有:

WITH PatCPlanRanked AS (
  SELECT
    Pat_ID1,
    Tx_Intent,
    Ranking = ROW_NUMBER() OVER (PARTITION BY Pat_ID1 ORDER BY Eff_DtTm DESC)
  FROM PatCPlan
),
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 PatCPlanRanked p
ON s.Pat_ID1=p.Pat_ID1 AND p.Ranking = 1
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;

我对您的表达式进行了一项更改Ranking:在 ROW_NUMBER 函数的 OVER 子句中,我更改了

ORDER BY ABS(DATEDIFF(dd, Eff_DtTm, GETDATE()))

到更sargable

ORDER BY Eff_DtTm DESC

Eff_DtTm永远不能晚于GETDATE(). 但我认为是这样,因为你表达了你的意图(强调

所以它只显示患者最新的Tx_Intent(Eff_DtTm)

并且您的表达只能在上述假设下匹配该意图。

于 2013-10-11T06:01:43.647 回答