使用按ID分组的CTE Sql Server 2008运行减法合计

使用按ID分组的CTE Sql Server 2008运行减法合计,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我需要基于行数的两个值之间的差异。CTE返回如下数据: 我想做的是让rehabWait列92-32中的第2行读60,第3行也读152-92`等等,直到patientid发生变化。所以对于第11排,我希望rehabwait是110 114-4。我的查询将运行,但我希望它返回所有空值 with x as ( SELECT row_number() over (order by patientid, admissiondate, claimsfromdate, datediff(

我需要基于行数的两个值之间的差异。CTE返回如下数据:

我想做的是让rehabWait列92-32中的第2行读60,第3行也读152-92`等等,直到patientid发生变化。所以对于第11排,我希望rehabwait是110 114-4。我的查询将运行,但我希望它返回所有空值

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
select中的两个CASE语句确保获得第一行。分区方式是确保每个患者Id从rn=1开始并上升


编辑:我给您的第一个查询是在您的示例中减少第1行和第10行。

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

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

你不需要这个行号。相关子查询可用于日期字段。

您可能需要编辑联接。我不完全确定录取日期是否也必须匹配。请看我的编辑,我给你的第一个查询并不是你想要的。
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