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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:34:55+00:00 2026-06-17T11:34:55+00:00

The heading of this question is probably poorly worded as I am finding it

  • 0

The heading of this question is probably poorly worded as I am finding it difficult to explain concisely what I want, other than to provide some demo data.

I have a query which returns the following data from a sql table:

ID    Job    User    Amount

1     101    Bob     100
2     101    Pete    500
3     102    Bob     400
4     102    Pete    200
5     101    Pete    850
6     102    Bob     650

What I want is the query to also return an additional field called (Difference), which contains the difference between the Amount in consecutive entries for the same User and Job. Hence the data I would like returned would be as follows:

ID    Job    User    Amount   Diff

1     101    Bob      100     100
2     101    Pete     500     500
3     102    Bob      400     400
4     102    Pete     200     200
5     101    Pete     850     350
6     102    Bob      650     250

In the first four rows, the Diff is the same as the Amount because each is the first entry per User per Job (hence the Difference is calculated with reference to a starting Amount of nil in effect).

The last two lines contain information for a User and Job combination that have appeared in the table previously, and hence Diff is calculated as follows:

Job 101    User Pete    850 - 500 = 350
Job 102    User Bob     650 - 400 = 250

I’ve never had to compare data from rows like this in a SQL query before so don’t really know where to start. Any help would be much appreciated.

Added

Please note the Amount is not a running total. It is a subjective assessment made periodically of the value of a User’s input in each particular job. It is possible that the Amount could in fact go down from one assessment to the next. What I want is a query that returns the difference between successive assessments ‘Amounts’.

Alternative Explanation

I’m looking to return a history trail of movements in the Amount assessed. So another example, looking at a single Job and User is as follows:

Job    User    Amount    Movement

101    Bob      100       100
101    Bob      500       400
101    Bob      400      (100)
101    Bob    1,000       600

However, as per the original example, this information will need to be extracted from a table which contains many Jobs and Users all intermingled.

  • 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-17T11:34:56+00:00Added an answer on June 17, 2026 at 11:34 am

    For SQL Server 2012, try this

    This assumes that ID=5 value is wrong in your example

    For “previous value” per pair

    DECLARE @t TABLE (ID int, Job int, Username varchar(10), Amount int);
    INSERT @t
    VALUES
        (1, 101, 'Bob', 100), (2, 101, 'Pete', 500), (3, 102, 'Bob', 400),
        (4, 102, 'Pete', 200), (5, 101, 'Pete', 850), (6, 102, 'Bob', 650);
    
    SELECT
        t1.*,
        t1.Amount - ISNULL(LAG(Amount) OVER (PARTITION BY Job, Username ORDER BY ID), 0) AS DiffAmount
    FROM
        @t t1
    ORDER BY
       t1.ID
    

    For “first value” per pair

    SELECT
        t1.*,
        CASE
            WHEN FIRST_VALUE(t1.ID) OVER (PARTITION BY Job, Username ORDER BY ID) = t1.ID THEN t1.Amount
            ELSE t1.Amount - FIRST_VALUE(t1.Amount) OVER (PARTITION BY Job, Username ORDER BY ID)
        END AS DiffAmount
    FROM
        @t t1
    ORDER BY
        t1.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like the answer to this question is probably very simple, but I'm
Sorry I couldn't find the best heading for this question. Following is my requirement.
I have a string like this: Heading Some interesting text here HeadingSome interesting text
Allright, this might be a strange question and maybe I am heading the wrong
Hopefully this question won't be too convoluted or vague. I know what I want
My question is probably easiest to explain in the form of an example: type
Probably a silly question, but I'm new to all this. I am creating a
I hate to give the question this heading but I actually don't know whats
This is going to be a vague and obscure question, which is probably due
I'm not sure if the heading of this question is even going to make

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.