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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:47:30+00:00 2026-06-03T08:47:30+00:00

I have asked a similar question previously, but posting a new one as I

  • 0

I have asked a similar question previously, but posting a new one as I do not want to confuse other
members, and there is an additional column.

What I am looking for is to update the column ItemActual. This needs to be updated with the difference with ItemValue for the latest CurrentTime for the same StartTime if any.

If there is no other entry for the same StartTime other than the current row, it needs to be the ItemValue itself. The comparisons are only to be done for items with the same name.

For eg, Rownum 283, ItemActual = 347013 (since there is no other row with same StartTime).
This applies to row 235 as well, i.e. ItemActual = 1086054.00

For row 190, this needs to be 664031.00 - 533023.00 (comparing with row 145) = 131008
But for row 10, this will be 532023.00, since there is no earlier entry of same item with same StartTime.

Rownum  Name  ItemValue      CurrentTime        StartTime
 283    ABC     347013.00     3/05/2012 16:01   29/04/2012 6:29
 235    ABC    1086054.00    26/03/2012 14:05    7/03/2012 21:18
 190    ABC     664031.00    13/02/2012 13:42   29/01/2012 6:39
 145    ABC     533023.00     7/02/2012 14:01   29/01/2012 6:39
 100    ABC     532023.00     7/02/2012 13:33   29/01/2012 6:39
  55    ABC     532023.00     7/02/2012 12:52   29/01/2012 6:39
  10    ABC     532023.00     7/02/2012 12:51   29/01/2012 6:39
 310    DEF     351012.00     3/05/2012 16:01   29/04/2012 6:29
 261    DEF    1339066.00    26/03/2012 14:05    7/03/2012 21:18
 215    DEF     785034.00    13/02/2012 13:42   29/01/2012 6:39
 170    DEF     620026.00     7/02/2012 14:01   29/01/2012 6:39
  • 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-03T08:47:31+00:00Added an answer on June 3, 2026 at 8:47 am

    You can try something like this:

    ;WITH PartData AS 
    (     
        SELECT
            RowNum, Name, ItemValue, CurrentTime, StartTime,
            RX = ROW_NUMBER() OVER(PARTITION BY Name,StartTime ORDER BY CurrentTime DESC)
        FROM dbo.YourTable
    )
    SELECT     
        p1.RowNum, p1.ItemValue, p1.CurrentTime, p1.StartTime, 
        RowNumComparedTo = p2.RowNum, 
        ItemActual = CASE 
                        WHEN p2.RX IS NULL THEN p1.ItemValue
                        ELSE p1.ItemValue - p2.ItemValue
                     END
    FROM PartData p1
    LEFT OUTER JOIN PartData p2 ON p1.StartTime = p2.StartTime 
                                   AND p1.Name = p2.Name 
                                   AND p2.RX = p1.RX + 1
    

    I get an output something like this:

    RowNum  ItemValue   CurrentTime       StartTime        RowNumComparedTo  ItemActual
     190     664031.00  2012-02-13 13:42  2012-01-29 06:39    145            131008.00
     145     533023.00  2012-02-07 14:01  2012-01-29 06:39    100              1000.00
     100     532023.00  2012-02-07 13:33  2012-01-29 06:39     55                 0.00
      55     532023.00  2012-02-07 12:52  2012-01-29 06:39     10                 0.00
      10     532023.00  2012-02-07 12:51  2012-01-29 06:39    NULL           532023.00
     215     785034.00  2012-02-13 13:42  2012-01-29 06:39    170            165008.00
     170     620026.00  2012-02-07 14:01  2012-01-29 06:39    NULL           620026.00
     235    1086054.00  2012-05-03 14:05  2012-03-07 21:18    NULL          1086054.00
     261    1339066.00  2012-03-26 14:05  2012-03-07 21:18    NULL          1339066.00
     283     347013.00  2012-05-03 16:01  2012-04-29 06:29    NULL           347013.00
     310     351012.00  2012-05-03 16:01  2012-04-29 06:29    NULL           351012.00
    

    The solution basically does this:

    • it creates a CTE (Common Table Expression) and “partitions” your data by Name,StartTime and orders these rows by CurrentTime DESC – so the most recent entry for each Name,StartTime group will get an RX (Row indeX) of 1

    • it then joins that CTE against itself, shifted by one RX – so I’m comparing RX = 1 to RX = 2 (if present) etc.

    • if a “shifted” row is present, the difference in the ItemValue values is returned as ItemActual – otherwise the ItemValue from the main row is returned

    I hope this solves your problem

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

Sidebar

Related Questions

I have asked a similar question previously, but 1. no one answered and 2.
Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
I know a similar question has been asked but I have not found a
I have asked a similar question to this one already but I think it
I have asked a similar question previously but it was never resolved so here
I have a problem similar to this question which was previously asked but my
Ok, I have previously asked a similar question to this but it was voted
I have asked a similar question but I didn't get much from that one.
I have asked a question similar to this in the past but this is
I have a similar question to the one I just asked. I'm trying to

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.