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

The Archive Base Latest Questions

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

I have a table used to enter weekly Hobbs hour meter readings for vehicles.

  • 0

I have a table used to enter weekly Hobbs hour meter readings for vehicles. I need to be able to add up the total hours used for each vehicle over a period of time. The problem I am having is that sometimes the meters get swapped out once they fail and are replaced with new meters the are not programmable and I have to start at zero hours so I will need to loop through each meter reading and determine if a meter had been swapped and account for it in my code. I’m not big on cursors but may have to use one to create a stored procedure that will work unless someone has a better solution I should try. I’m trying to think of a way a CTE might help but nothing yet.

The table structure is:

ReadingDate Datetime
EquipmentId Int
MeterReading  decimal(18,2)

I.m planning on Creating a accumulation variable, loop through the rows in a cursor and add the reading difference into the variable.
So as I loop through and see a meter reading is less than the previous reading, I must have a meter swap out and will have to account for it. I’m thinking that if I was to look ahead at the next reading and it is lower than the current, I would just skip the summing action one time and then start adding the difference to the accumulation variable. Any better way to do this? Thanks.

  • 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-13T00:08:41+00:00Added an answer on June 13, 2026 at 12:08 am

    This will work in SqlServer:

    SELECT t1b.EquipmentId, 
           t1a.ReadingDate As StartDate, 
           t1b.ReadingDate AS EndDate,
           CASE 
             WHEN t1b.Reading >= t1a.Reading THEN t1b.Reading - t1a.Reading 
             ELSE t1b.Reading
          END AS Hours
    FROM MeterReadings AS t1a
    JOIN (select t11.EquipmentId, 
                 t11.ReadingDate AS Date1, 
                 (SELECT MIN(t13.ReadingDate)  
                    FROM MeterReadings AS t13 
                   WHERE t13.EquipmentId = t11.EquipmentId 
                    AND t13.ReadingDate > t11.ReadingDate 
               Group By t13.EquipmentId) AS NextReadingDate
           from MeterReadings AS t11) AS rd ON t1a.EquipmentId = rd.EquipmentId 
                                            AND t1a.ReadingDate = rd.Date1
    JOIN MeterReadings as t1b ON t1b.EquipmentId = t1a.EquipmentId 
                              AND t1b.ReadingDate = rd.NextReadingDate
    order by t1a.EquipmentId, t1a.readingdate
    

    SQL Fiddle I was playing with

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

Sidebar

Related Questions

We have a table that is used for assessment-values in our tool where each
Basically I have a table which is used to hold employee work wear details.
Given I have table with some data... I have a query which is used
I have this JSF table which is used to display data. I want to
I have a table A which references table B. edit: Database engine used is
I used JPA and mysql. Each table have entity class.I have four tables. Table1
I have a huge table that is mainly used for backup and administrative purposes.
I have a oracle table A which contains a column A.a which used to
I have a pretty common layout issue that I have traditionally used a table
I have a table that's used to store some temporary bookkeeping stuff. It needs

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.