Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8328919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:35:45+00:00 2026-06-09T01:35:45+00:00

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

  • 0

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

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:35:47+00:00Added an answer on June 9, 2026 at 1:35 am
    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 
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find to the time difference between two values, but the values
I need to be able to compute differences between two data/time values which represent
I need to convert a time difference (diff) between two actions into a human
So, I need to find out the highest possible difference between any two random
I need a function that can return the difference between the below two dates
I have a need to sync auto_increment fields between two tables in different databases
I need to find the number of days between two dates : one is
What is difference between these two? Which one you would prefer when you need
I have to sort a number of integers, which can have values between 30.000.000
I have two tables that need the exact same values for denormalization purposes. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.