2012-08-03 64 views
2

按CTE Sql Server 2008計算的總減法總數我需要基於它們的行號的兩個值之間的差異。像 enter image description here按照ID#

的CTE返回的數據我想要做的是有2行中rehabWait列(92-32)讀60和排三60以及(152-92`)等等,直到有一個在patientid改變。因此,對於第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 
+0

請參閱我的編輯,我給你的第一個查詢並不完全符合你的要求。 – Aushin 2012-08-03 18:01:12

回答

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行。

+0

您可能想要編輯JOIN。我不完全確定入場人員是否也必須匹配。 – Aushin 2012-08-03 17:41:28

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()這一點。相關的子查詢可以在日期字段上工作。