2

行番号に基づいて 2 つの値の違いが必要です。CTE は次のようなデータを返します ここに画像の説明を入力

私がやりたいことは60、患者 ID が変更されるまで、行 2 を rehabWait 列 (92-32) で読み取り、行 3 も 60 (152-92`) にすることです。したがって、行 11 では、rehabwait を 110 (114-4) にしたいと思います。私が持っているクエリは実行されますが、すべてNULLを返します

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 

select 内の 2 つの 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 に答える