I need a difference between two values based on their row number. The CTE returns data like

What I want to do is have row2 read 60 in the rehabWait column (92-32) and row three to be 60 as well (152-92`) etc. until there is a change in patientid. So for row 11 I’d like for rehabwait to be 110 (114-4). The query I have will run but I it returns all 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
The two CASE statements in the select are making sure that you get the first rows. The PARTITION BY is making sure that every patient Id starts at rn = 1 and ascends.
EDIT: The first query I gave you was going to lose rows 1 and 10 in your example.