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

  • Home
  • SEARCH
  • 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 309887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:46:59+00:00 2026-05-12T07:46:59+00:00

I have a data set consisting of time-stamped values, and absolute (meter) values. Sometimes

  • 0

I have a data set consisting of time-stamped values, and absolute (meter) values. Sometimes the meter values reset to zero, which means I have to iterate through and calculate a delta one-by-one, and then add it up to get the total for a given period.

For example:

Timestamp      Value
2009-01-01     100
2009-01-02     105
2009-01-03     120
2009-01-04     0 
2009-01-05     9

the total here is 29, calculated as:

(105 - 100) + (120 - 105) + (0) + (9 - 0) = 29

I’m using MS-SQL server for this, and open to any suggestions.

Right now, I’m using a cursor to do this, which checks that the delta isn’t negative, and then totals it up:

DECLARE CURSOR curTest CURSOR FAST_FORWARD FOR
    SELECT value FROM table ORDER BY timestamp
OPEN curTest
DECLARE @delta bigint, @current bigint, @last bigint
SET @delta = 0

FETCH curTest INTO @current
WHILE @@FETCH_STATUS = 0
BEGIN
    IF (@current IS NOT NULL) AND (@current > 0) 
    BEGIN
        IF (@last IS NOT NULL) AND (@current > @last)
            SET @delta = @delta + (@current - @last)
        SET @last = @current

        FETCH curTest INTO @current
    END
END

CLOSE curTest
DEALLOCATE curTest

It would be nice to get a data set like:

Timestamp      Value    LastValue
2009-01-01     100      NULL
2009-01-02     105      100
2009-01-03     120      105
2009-01-04     0        120
2009-01-05     9        0

as then it would be easy to grab the deltas, filter for (Value > LastValue), and do a SUM().

I tried:

SELECT m1.timestamp, m1.value, 
  ( SELECT TOP 1 m2.value FROM table WHERE m2.timestamp < m1.timestamp ORDER BY m2.timestamp DESC ) as LastValue
FROM table 

but this actually turns out to be slower than the cursor: When I run these together in SQL studio with ‘show execution plan’ on, the relative cost of this is 100% (with 7 or 8 operations – the majority in a clustered index scan on timestamp), and the cursor is 0% (with 3 operations).

(What I’m not showing here for simplicity is that I have several different sets of numbers, with a foreign key in this table as well – so there is also always a WHERE clause limiting to a specific set. I have several places where I calculate these totals for a given time period for several sets at once, and thus it becomes quite the performance bottleneck. The non-cursor method can also be easily modified to GROUP BY the key and return all the sets at once – but this actually is even slower in my testing than running the cursor multiple times, because there is the additional overhead of the GROUP BY and SUM() operation, aside from it being slower overall anyways.)

  • 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-12T07:46:59+00:00Added an answer on May 12, 2026 at 7:46 am

    Much the same…

    create table #temp ([timestamp] date,value int);
    insert into #temp (timestamp,value) values ('2009-01-01',100)
    insert into #temp (timestamp,value) values ('2009-01-02',105)
    insert into #temp (timestamp,value) values ('2009-01-03',120)
    insert into #temp (timestamp,value) values ('2009-01-04',0)
    insert into #temp (timestamp,value) values ('2009-01-05',9);
    
    with numbered as
    (
        select ROW_NUMBER() over (order by timestamp) id,value from #temp
    )
    select sum(n1.value-n2.value) from numbered n1 join numbered n2 on n1.id=n2.id+1 where n1.value!=0
    
    drop table #temp;
    

    Result is 29, as specified.

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

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this - you just need to use the backreferences… May 12, 2026 at 10:48 am
  • Editorial Team
    Editorial Team added an answer I asked a friend at Google and they were able… May 12, 2026 at 10:48 am
  • Editorial Team
    Editorial Team added an answer I believe that you'll have to add the entire structure… May 12, 2026 at 10:48 am

Related Questions

Report design, generation and maintenance isn't hard, but it is dull. We have a
I have three tables called: users , facilities , and staff_facilities . users contains
I've got an application here that I wrote many years ago that consists of
I have a specific case in mind, but the question applies in general too.
Ok, so here is a problem analogous to my problem (I'll elaborate on the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.