简体   繁体   中英

Obtaining Result Based on Date Field of Another Column

The below sql server 2005 query is currently working successfully. I need to modify the "p.Tx_Intent" to display based of a date column "p.Eff_DtTm" in the same table, so it only displays the latest Tx_Intent(Eff_DtTm) for the patient.

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;

Ok so i figured out a way to determine the most recent date im just having issues combinding it with my current query. Here is the query i need combined with the top query

   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

Thanks for you help!

Not entirely sure but here goes:

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  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;

I made one change to your Ranking expression: in the OVER clause of the ROW_NUMBER function, I changed the

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

to the more sargable

ORDER BY Eff_DtTm DESC

That is equivalent to your version only if Eff_DtTm can never be later than GETDATE() . But I assumed that to be the case because you expressed your intention as ( emphasis added )

so it only displays the latest Tx_Intent(Eff_DtTm) for the patient

and your expression can match that intention only under the above mentioned assumption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM