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 790691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:43:17+00:00 2026-05-14T21:43:17+00:00

Suppose I have a table with columns (DayId, RunningTotal): DayId RunningTotal ——————— 1 25

  • 0

Suppose I have a table with columns (DayId, RunningTotal):

DayId    RunningTotal
---------------------
1        25
3        50
6        100
9        200
10       250

How can I select the DayId and the amount the RunningTotal has increased from the previous day? i.e. how can I select:

DayId    DayTotal
---------------------
1        25
3        25
6        50
9        100
10       50

The only current method I know is with a while loop I am trying to factor out. Also, the DayId has no regular rules, just that it is some increasing integer value, but it increases by an irregular amount as shown in the example table.

EDIT: using MS SQL Server 2005

  • 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-05-14T21:43:18+00:00Added an answer on May 14, 2026 at 9:43 pm
    with cte as (
      select dayid, runningtotal, row_number() over (order by dayid asc) as row_index
      from #the_table
    )
    select cur.dayid, cur.runningtotal - coalesce(prev.runningtotal, 0) as daytotal
    from cte cur
         left join cte prev on prev.row_index = cur.row_index - 1
    

    (I really wish they’d implemented support for the lead and lag functions in SQL Server :|)

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

Sidebar

Related Questions

Suppose I have a table: ACCOUNT It has columns: ID , NAME , BALANCE
Suppose I have table with two columns. I can center this table by using:
Suppose I have a database table with columns a, b, and c. I plan
Suppose I have two columns in a table that represents a graph, the first
Suppose I have table A with a field that can be either 1 or
Suppose I have a table abc with columns p_id , u_id , and comments
Suppose I have a table amountInfo with columns (id, amount1, amount2, amount3) where amountX
Suppose we have a table with different columns representing properties of an object e.g.
Suppose I have a table with the following columns (a list of words): word:
Suppose I have a table called 'test', into which there are three columns named

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.