pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Brent Wood <pcreso@yahoo.com>
To: Brent Wood <brent.wood@niwa.co.nz>
To: Michael Lewis <mlewis@entrata.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Timestamp/hstore query?
Date: Wed, 14 Apr 2021 21:33:03 +0000 (UTC)
Message-ID: <288622710.2086083.1618435983252@mail.yahoo.com> (raw)
In-Reply-To: <CAHOFxGqbjSOuzzTwDAG=YEQ07rd-WszvBOq47PAiTLZNZwdjqA@mail.gmail.com>
References: <SY3PR01MB081006506D32FA0D7823B239A14E9@SY3PR01MB0810.ausprd01.prod.outlook.com>
	<CAHOFxGqbjSOuzzTwDAG=YEQ07rd-WszvBOq47PAiTLZNZwdjqA@mail.gmail.com>

Thanks for your reply,
The table is essentially:create table readings (timer    timestamp primary key,                       readings  hstore);

the hstore comprises (<sensor_id> <reading>) key/value pairs for readings taken at the time specified in the timestamp.
eg:  "67" "-45.67436", "68" "176.5424" could be key/value pairs representing latitude & longitude, with a timestamp in the timer column.

There would be several lat/lon hstore pairs in a given minute, the query I want would return the last one in the timeseries for that minute (for each key).

I don't think your examples will give me the separate hstore key-value pairs, extracted as the last in the interval for each key & reassembled as an hstore list in the result. The sensor id is the hstore key, as described above, not a separate attribute. That said, the keys can be extracted from the hstore much like a column, but I'm not sure that is the best approach.
Treating each set of hstores in an interval as an array & extracting the last elements may be viable. But I['m not sure how...

 
 
 On Thursday, April 15, 2021, 6:33:08 AM GMT+12, Michael Lewis <mlewis@entrata.com> wrote: 





If you share example schema and desired output (like a dummy table or even pseudo code SQL), then I'm sure many people could help you. Right now, the description of your desired result seems a bit unclear, at least to me.


If you wanted to run this hourly for the last 1 hour, it sounds a bit like want this-

select sensor_id, date_trunc( 'minute', timestamptz_field_name_here ), last( value_from_hstore ) over ( partition by sensor_id, date_trunc( 'minute', timestamptz_field_name_here ) ) as last_value_recorded
from data_table_here
where timestamptz_field_name_here BETWEEN NOW() - interval '1 hour' and NOW()
group by sensor_id, date_trunc( 'minute', timestamptz_field_name_here )


You could also use the left join "where is null" pattern to check that a prior record in the minute period of time does not exist for that same key. Something like this-

select d1.sensor_id, date_trunc( 'minute', d1.timestamptz_field_name_here ), d1.timestamptz_field_name_here as last_value_recorded
from data_table_here as d1
left join data_table_here as prior_d1 ON prior_d1.sensor_id = d1.sensor_id AND prior_d1.timestamptz_field_name_here < d1.timestamptz_field_name_here and prior_d1.timestamptz_field_name_here >= date_trunc( 'minute', d1.timestamptz_field_name_here )
where d1.timestamptz_field_name_here BETWEEN NOW() - interval '1 hour' and NOW()


>  
 

view thread (5+ messages)  latest in thread

Message-ID: <288622710.2086083.1618435983252@mail.yahoo.com>
Permalink:  ../288622710.2086083.1618435983252@mail.yahoo.com/
Also on:    postgresql.org/message-id/288622710.2086083.1618435983252@mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pcreso@yahoo.com, brent.wood@niwa.co.nz, mlewis@entrata.com
  Subject: Re: Timestamp/hstore query?
  In-Reply-To: <288622710.2086083.1618435983252@mail.yahoo.com>
  References: <SY3PR01MB081006506D32FA0D7823B239A14E9@SY3PR01MB0810.ausprd01.prod.outlook.com> <CAHOFxGqbjSOuzzTwDAG=YEQ07rd-WszvBOq47PAiTLZNZwdjqA@mail.gmail.com> <288622710.2086083.1618435983252@mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox