基于另一列日期字段的获取结果

时间:2013-10-10 07:32:11

标签: sql sql-server-2005 date

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

谢谢你的帮助!

1 个答案:

答案 0 :(得分: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)

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