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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:21:56+00:00 2026-05-18T00:21:56+00:00

I have a table with this schema: DeviceID int FloorID int RoomID int DateRecorded

  • 0

I have a table with this schema:

DeviceID int
FloorID int
RoomID int
DateRecorded datetime
RecordedValue decimal

A sample of the table’s data looks like:

DeviceID   FloorID   RoomID   DateRecorded RecordedValue
0001       Floor 1   Room 1   1/1/2000     0 
0001       Floor 1   Room 1   1/2/2000     10.5000
0001       Floor 1   Room 1   1/3/2000     18.7500  
0002       Floor 1   Room 2   1/1/2000     10.0000 
0002       Floor 1   Room 2   1/2/2000     10.0000
0002       Floor 1   Room 2   1/3/2000     22.5000  

I need to build a query that will give me the RecordedValue range each device recorded for each date.

Something like:

DeviceID   FloorID   RoomID   DateRecorded  StartValue  EndValue
0001       Floor 1   Room 1   1/1/2000      NULL        0 
0001       Floor 1   Room 1   1/2/2000      0.0001      10.5000 
0001       Floor 1   Room 1   1/3/2000      10.5001     18.7500  
0002       Floor 1   Room 2   1/1/2000      NULL        10.0000 
0002       Floor 1   Room 2   1/2/2000      10.0000     10.0000 
0002       Floor 1   Room 2   1/3/2000      10.0001     22.5000   

So basically, it has to take the MIN from the previous day’s recording if exists or NULL and the MAX from the next day if exists or NULL.

Each device records the accumulated value each day.

Note the issue when the device records the same value for a few days…
Also don’t assume that we get a reading every day. We may have gaps in the days recorded.

  • 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-18T00:21:57+00:00Added an answer on May 18, 2026 at 12:21 am

    Your question isn’t entirely clear. You appear to be computing the start and end values from the previous day’s values according to some formula, but you don’t state what the formula is. for instance, it’s not clear to me why Dev 2 on 1/2/2000 has an end value of 22.4999 when the recorded value is 10.000.

    Also, you don’t state whether each device is limited to a single record per day.

    Assuming the following:

    1. Each device has only one reading per day.
    2. There is a reading every single date.
    3. The StartValue for day X is .0001 more than the EndValue for day X-1.

    then you can use something like the following query:

    SELECT D1.DeviceID, D1.FloorID, D1.RoomID, 
           D1.DateRecorded, D2.RecordedValue + .0001 AS StartValue,
           D1.RecordedValue AS EndValue
      FROM YourTable D1 INNER JOIN YourTable D2
      ON D1.DeviceID = D2.DeviceID AND D1.FloorID = D2.FloorID AND 
         D1.RoomID = D2.RoomID AND D1.DateRecorded = D2.DateRecorded+1
    

    This will problems with the first or last day in the data set. You can solve that problem by switching to a LEFT or RIGHT OUTER JOIN, depending on whether you want the first or last date to appear.

    Also note that you should factor out FloorID and RoomID from your recorded values table since they appear to depend on DeviceID. This will normalize your database, reduce storage, and simply the JOIN conditions in the query.

    Edit in response to comments, below:

    If assumption #2 (there’s a record for every day) is not valid, then you can use something like this revised version (also changed not to use the .0001 to generate a false start value):

    SELECT D1.DeviceID, D1.FloorID, D1.RoomID, 
           D1.DateRecorded, D2.RecordedValue AS StartValue,
           D1.RecordedValue AS EndValue
      FROM YourTable D1 INNER JOIN YourTable D2
      ON D1.DeviceID = D2.DeviceID AND D1.FloorID = D2.FloorID AND 
         D1.RoomID = D2.RoomID AND D2.DateRecorded = 
           (SELECT MAX(DateRecorded) FROM YourTable 
              WHERE DeviceID = D1.DeviceID AND FloorID = D1.FloorID AND 
                   RoomID = D1.RoomID AND DateRecorded < D1.DateRecorded)
    

    And I’ll just rewrite this to show you how much smaller it is if you factor out the non-normalized columns:

    SELECT D1.DeviceID, D1.FloorID, D1.RoomID, 
           D1.DateRecorded, D2.RecordedValue AS StartValue,
           D1.RecordedValue AS EndValue
      FROM YourTable D1 INNER JOIN YourTable D2
      ON D1.DeviceID = D2.DeviceID AND D2.DateRecorded = 
           (SELECT MAX(DateRecorded) FROM YourTable 
              WHERE DeviceID = D1.DeviceID AND DateRecorded < D1.DateRecorded)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with this schema: DeviceID int RoomID int DateInstalled datetime A
Say I have this table schema. ID AccNo Amount Say I have this data
I have a table named Info of this schema: int objectId; int time; int
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
I have table one which looks like this. And I want to get data
I currently have this schema: CREATE TABLE `users` ( `users_id` int(11) NOT NULL AUTO_INCREMENT,
I have an orders table with a schema like this. CREATE TABLE orders (
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
If I have a table with a schema like this table(Category, SubCategory1, SubCategory2, Status)
I've defined table with this schema : CREATE TABLE [dbo].[Codings] ( [Id] [int] IDENTITY(1,1)

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.