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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:36:40+00:00 2026-06-13T10:36:40+00:00

I have this query that calculates current gallons value from all fuel tanks in

  • 0

I have this query that calculates current gallons value from all fuel tanks in my database.

SELECT DISTINCT y.TankNumber as TankNumber
    , y.Gallons as Gallons
    , y.timeUpdated
    , y.FuelType as FuelType
FROM (
    SELECT TankNumber, max(timeUpdated) as maxdate
    FROM someTable
    GROUP BY TankNumber) as x
JOIN someTable y
ON x.TankNumber = y.TankNumber
AND x.maxdate = y.timeUpdated
ORDER BY y.TankNumber

Based on the fuel usage, data gets dumped in to my database automatically at any time. And query above will give me only the current gallons value in each fueltank:

TankNumber  |  Gallons  |  timeUpdated        |  FuelType
     1      |  14       |  2012-10-22 04:16   |  89
     2      |  8        |  2012-10-22 04:14   |  93

and etc..
My problem is, that I am trying to add another output value to my page, that will give me a difference how much fuel was used since last update. So it will look something like this:

TankNumber  |  Gallons  |  timeUpdated        |  FuelType  |  GallonsUsed
     1      |  14       |  2012-10-22 04:16   |  89        |  5
     2      |  8        |  2012-10-22 04:14   |  93        |  -11

Unfortunately my SQL experience is not as solid for this type of problem and I have spent about two days trying to figure out or google something close. So, any help will be greatly appreciated.

  • 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-13T10:36:42+00:00Added an answer on June 13, 2026 at 10:36 am

    Assuming you’re using MS SQL 2005 or later, you can use the ROW_NUMBER function:

    WITH cteOrderedUpdates As
    (
       SELECT
          TankNumber,
          Gallons,
          TimeUpdated,
          FuelType,
          ROW_NUMBER() OVER 
          (
             PARTITION BY 
                TankNumber 
             ORDER BY 
                TimeUpdated DESC
          ) As RowNumber
       FROM
          someTable
    )
    SELECT
       x.TankNumber,
       x.Gallons,
       x.TimeUpdated,
       x.FuelType,
       x.Gallons - IsNull(y.Gallons, 0) As GallonsUsed
    FROM
       cteOrderedUpdates As x
       LEFT JOIN cteOrderedUpdates As y
       ON x.TankNumber = y.TankNumber
       And x.RowNumber = y.RowNumber - 1
    WHERE
       x.RowNumber = 1
    ORDER BY
       x.TankNumber
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have this working query that outputs URL and click counts from database. <?php
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
I have this SQL Query that pulls data from 3 tables. I am unable
I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume
In my webapp, have a JSON data response from a database query that includes
I have this query that returns the results by ordering it by focus.name ASC.
I have this query that, due to the number of records, is taking several
I have this query that inserts rows, using a subquery like so: INSERT INTO
I have this linq query that works well (although it may be written better,

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.