2

我需要根据它们的行号来区分两个值。CTE 返回数据,如 在此处输入图像描述

我想要做的是60在 rehabWait 列(92-32)中读取第 2 行,第 3 行也为 60(152-92`)等,直到患者 ID 发生变化。所以对于第 11 行,我希望 rehabwait 是 110 (114-4)。我的查询将运行,但我返回所有 NULLS

with x as 
    (
     SELECT row_number() over (order by patientid, admissiondate, claimsfromdate, datediff(dd,admissiondate, claimsfromdate))as rn,
     patientid, admissiondate, claimsfromdate,
            DATEDIFF(dd, admissiondate, claimsfromdate) as rehabWait, hcpcs
     FROM    Claims
     WHERE   hcpcs in ('g0151', '97001', '97002', '9339') and
             claimsfromdate > admissiondate 
      group by patientid, admissiondate, claimsfromdate, hcpcs
      --adding this group by clause will keep rehabWait from showing up
      --however many times they patient has the HCPCS code for rehab
    )


select  x.patientid
        ,x.admissiondate
        ,x.claimsfromdate

        ,(select x2.rehabWait-x.rehabwait from x where x.patientid=x2.patientid 
        and x.rn > x2.rn and x.admissiondate=x2.admissiondate and x.claimsfromdate=x2.claimsfromdate
        )
        from x inner join
        x as x2 on x.patientid=x2.patientid and x.admissiondate=x2.admissiondate and x.claimsfromdate = x2.claimsfromdate
4

2 回答 2

2
with x as 
(
 SELECT row_number() over (PARTITION BY patientid order by patientid, admissiondate, claimsfromdate, datediff(dd,admissiondate, claimsfromdate))as rn,
 patientid, admissiondate, claimsfromdate,
        DATEDIFF(dd, admissiondate, claimsfromdate) as rehabWait, hcpcs
 FROM    #Claims
 WHERE   hcpcs in ('g0151', '97001', '97002', '9339') and
         claimsfromdate > admissiondate 
  group by patientid, admissiondate, claimsfromdate, hcpcs
  --adding this group by clause will keep rehabWait from showing up
  --however many times they patient has the HCPCS code for rehab
)
select  x.patientid
    ,x.admissiondate
    ,x.claimsfromdate
    , CASE WHEN x2.rn = 1 
           THEN x.rehabWait 
           ELSE  x2.rehabWait - x.rehabWait END AS rehabWait
    from x INNER join
    x as x2 on x.patientid=x2.patientid AND CASE WHEN x2.rn = 1 AND x.rn = 1 THEN x2.rn - 1 ELSE x.rn END = x2.rn - 1 

选择中的两个 CASE 语句确保您获得第一行。PARTITION BY 确保每个患者 ID 从 rn = 1 开始并上升。

编辑:我给您的第一个查询将在您的示例中丢失第 1 行和第 10 行。

于 2012-08-03T17:38:50.980 回答
0

由于康复重量正在增加,您可以执行以下操作:

with x as 
    (
     SELECT patientid, admissiondate, claimsfromdate,
            DATEDIFF(dd, admissiondate, claimsfromdate) as rehabWait, hcpcs
     FROM    Claims
     WHERE   hcpcs in ('g0151', '97001', '97002', '9339') and
             claimsfromdate > admissiondate 
      group by patientid, admissiondate, claimsfromdate, hcpcs
  --adding this group by clause will keep rehabWait from showing up
  --however many times they patient has the HCPCS code for rehab
    )
select patientid, admissiondate, claimsfromdate,
       (RehabWait - prevRehabWait), hcpcs
from (select patientid, admissiondate, claimsfromdate, hcpcs, RehabWait,
             (select max(RehabWait)
              from x x2
              where x2.patientid = x.patientid and x2.claimsfromdate < x.claimsfromdate
             ) as prevRehabWait
      from x
     ) t

为此,您不需要 row_number() 。相关子查询可以在日期字段上工作。

于 2012-08-03T17:42:34.397 回答