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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:58:36+00:00 2026-06-05T08:58:36+00:00

My Table has these fields: SENSOR_ID INTEGER, SENSOR_READING REAL, TIME_OF_READING TIMESTAMP, HASCHANGED BOOLEAN Each

  • 0

My Table has these fields:

SENSOR_ID INTEGER, 
SENSOR_READING REAL, 
TIME_OF_READING TIMESTAMP, 
HASCHANGED BOOLEAN

Each sensor fires >10’000 readings/day. I want to log only those datapoints where SENSOR_READING is different from the last logged value.

Alternatively, I may log everything but set HASCHANGED to true whenever SENSOR_READING is different from the last logged value.

What is the most performant syntax for accomplishing that (with PostgreSQL, without php logics)?

  • 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-05T08:58:39+00:00Added an answer on June 5, 2026 at 8:58 am

    You could do something like this

    insert into readings (sensor_id, sensor_reading, time_of_reading)
    select 42, 1234.5678, current_timestamp
    from readings
    where not exists (select * 
                      from readings 
                      where sensor_reading = 1234.5678
                        and time_of_reading = (select max(time_of_reading)
                                               from readings
                                               where sensor_id = 42);
    

    This will require the sensor_reading column to be indexed but I still doubt that it will actually be faster.

    You don’t need to store the haschanged attribute because you can calculate that while retrieving the data:

    select sensor_id,
           sensor_reading,
           time_of_reading,
           lag(sensor_reading) over (partition by sensor_id order by time_of_reading) = sensor_reading as has_changed
    from readings;
    

    This assumes that sensor_id isn’t actually unique, otherwise you couldn’t store more than one reading for a sensor

    It further assumes that you change that REAL column to a NUMERIC column, because REAL values using = isn’t accurate (actually the storing isn’t accurate to begin with)

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

Sidebar

Related Questions

My table has three boolean fields: f1, f2, f3. If I do SELECT *
let's assume I have this numrequest table. it has these fields: id , id_user
I have a table of reports that has these relevant fields: user_id (int 11)
I have a user profile table that has 32 fields. These fields are always
I have a price history table that has these 4 fields Id Date Product
Mysql database question. There is system with objects(Domains). Each domain has own table. All
I have these two tables: Galleries - {id,title,description} Images - {id,gallery_id,file} Each gallery has
My application has one table called 'events' and each event has approx 30 standard
I've downloaded WIPmania's worldip table from http://www.wipmania.com/en/base/ -- the table has 3 fields and
I am designing a table that has several fields that will be used 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.