Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWn8N-0002SU-Ia for pgsql-general@arkaria.postgresql.org; Wed, 14 Apr 2021 21:33:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWn8M-0008Bn-Fl for pgsql-general@arkaria.postgresql.org; Wed, 14 Apr 2021 21:33:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWn8L-0008Bf-SK for pgsql-general@lists.postgresql.org; Wed, 14 Apr 2021 21:33:14 +0000 Received: from sonic311-24.consmr.mail.ne1.yahoo.com ([66.163.188.205]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lWn8I-0002NT-Gi for pgsql-general@lists.postgresql.org; Wed, 14 Apr 2021 21:33:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1618435986; bh=W0y6pqW/Pq8Hn9+zskm64slS27lnJ3wKiIdfPx529bQ=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From:Subject:Reply-To; b=RNFPkFU5RYIq2aUBhPEpMY/tH41zZzKwoI1ZjgKgJELBgc1P1mfjL9aMumy1P8xa+Jt3tWjU+tpvI0XWkk0LYICofPnIPCiVk7KJJHSSGFL4uDumqDmYrJxMkUpP+RHcw39icCbHwNoQYMa1+h3jqpRIcLvPe3Ky+CJ9jxDRfyUJRx88a3sDn8AmpL7FkK5jVCImetLyRMV7rGM8Id0wH9W6lIUuJDCSswrus/nFvO+R6zJ8btAEpUkwUuu8DZEkIUTFHx8GsdHgzc/0R8kvCw+UW4OIGmJxoJMwSmd5UB6tIcIoSr5tAtSakpntsNRCqiMDUyF+now/moYyI8F1KQ== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1618435986; bh=M28z4q7e614hGZZGFqCROEI/r+Vn0EuF2cDl/90cdxI=; h=X-Sonic-MF:Date:From:To:Subject:From:Subject; b=uUbr9DLCqBKBSNO57GPELsfy38T0Kj1df2v1vlNSGXQGOv9CwvT7iCso1RbgSMNpyw5pvZe8FseBO1Xv1T/LWeQS8pVNuHYpqINv//dzX38tt2N+uxWsrBQqhStgDe3Tj3doQgWZ9LoLdn0U8CGqSFoY5o6/grGwcd5a5JGJM9zrMx5eAUS/A/yqvIgQ9/bicfJMlOt7VRbZovBxBk9qIFFXlE4iUk3ox63G3nLc/EglNrsv1IynzAyMGtMgN5R2Fm+buwj8dcLWbGv9AW35ptp1U8pzQ2sdrLDRK+ARSsov22n3sHlHkJUiliSn+2WkyIOnf/qnIq0G1jAXY1i58Q== X-YMail-OSG: YmFJNjEVM1lSIaM_y8ZvqpHmUGunYHKLtAwbb6VQhpR79Jct7_ASI7catJNp4xq eyV0DQiRp9vfint8KKUEqTXuvfI.vyjGzzmohqvBRV8tLjSZgqo_W7rEb2VJCmSWRZxQs1VAqHpZ BiMWyLm3LR1TxHDQImnLj.ZnGDFhQDPchIqNHDueaYBOcK1X2CoxEmVCb0FVDASseAvczvoc1hxY AaR7dkvYwcTFUxdL0lPVG5Xw8yMpTrOJpyixIui640PSo52eSDqEB5WsQ9Yo8opZXveu80pH_Q0l NV3bX54umKV85fFdgK5jJdcGxmCha.vr2g2UXlzUFXZXRmf45SjBCHkA3IIs0KEW3QuM2GfZ8Bbi KAdDk2_7apw46ivBg7i5o7vELkvmJB1jfWTTisNKmciZB7Tx42G1ergFvTcHBQKtQGmEWbuD1N9b okkuFiIMo95W4m.pe_YGgrM6QXbIEHcq.dCmOrKcwkbgSVo8vrjTL14QuuxRW3mRLLFqsuUvmHak _wf9p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ail.ne1.yahoo.com by sonic311.consmr.mail.ne1.yahoo.com with HTTP; Wed, 14 Apr 2021 21:33:06 +0000 Date: Wed, 14 Apr 2021 21:33:03 +0000 (UTC) From: Brent Wood To: Brent Wood , Michael Lewis Cc: "pgsql-general@lists.postgresql.org" Message-ID: <288622710.2086083.1618435983252@mail.yahoo.com> In-Reply-To: References: Subject: Re: Timestamp/hstore query? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2086082_898197550.1618435983251" X-Mailer: WebService/1.1.18121 YMailNorrin Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0 Content-Length: 6924 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------=_Part_2086082_898197550.1618435983251 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thanks for your reply, The table is essentially:create table readings (timer=C2=A0=C2=A0=C2=A0 tim= estamp primary key,=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 readings=C2=A0 hstore); the hstore comprises ( ) key/value pairs for readings t= aken at the time specified in the timestamp. eg:=C2=A0 "67" "-45.67436", "68" "176.5424" could be key/value pairs repres= enting 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 pair= s, extracted as the last in the interval for each key & reassembled as an h= store list in the result. The sensor id is the hstore key, as described abo= ve, 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 la= st elements may be viable. But I['m not sure how... =20 =20 On Thursday, April 15, 2021, 6:33:08 AM GMT+12, Michael Lewis wrote:=20 If you share example schema and desired output (like a dummy table or even = pseudo=C2=A0code 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,=C2=A0date_trunc( 'minu= te', 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=C2=A0sensor_id, date_trunc( 'minute', timestamptz_field_name_here = ) You could also use the left join "where is null" pattern to check that a pr= ior record in the minute period of time does not exist for that same key. S= omething 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=C2=A0data_table_here as prior_d1 ON=C2=A0prior_d1.sensor_id =3D d= 1.sensor_id AND=C2=A0prior_d1.timestamptz_field_name_here < d1.timestamptz_= field_name_here and=C2=A0prior_d1.timestamptz_field_name_here >=3D=C2=A0dat= e_trunc( 'minute', d1.timestamptz_field_name_here ) where d1.timestamptz_field_name_here BETWEEN NOW() - interval '1 hour' and = NOW() >=C2=A0=C2=A0 =20 ------=_Part_2086082_898197550.1618435983251 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Thanks for your reply,

The table is essent= ially:
create table readings (timer    timesta= mp primary key,
&nbs= p;            &= nbsp;         readings = hstore);

the hstore co= mprises (<sensor_id> <reading>) key/value pairs for readings ta= ken at the time specified in the timestamp.

eg:  "67= " "-45.67436", "68" "176.5424" could be key/value pairs representing latitu= de & longitude, with a timestamp in the timer column.

There would be several lat/lon hstore pairs in a given minute, the que= ry 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 a= n 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.<= /div>

Treating each set of hstores in an interval as an array &= amp; extracting the last elements may be viable. But I['m not sure how...



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





If you = share example schema and desired output (like a dummy table or even pseudo&= nbsp;code SQL), then I'm sure many people could help you. Right now, the de= scription 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 BETWE= EN NOW() - interval '1 hour' and NOW()
group by sensor_id, date_tru= nc( 'minute', timestamptz_field_name_here )


You could also use t= he left join "where is null" pattern to check that a prior record in the mi= nute period of time does not exist for that same key. Something like this-<= br>
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 pri= or_d1.sensor_id =3D d1.sensor_id AND prior_d1.timestamptz_field_name_h= ere < d1.timestamptz_field_name_here and prior_d1.timestamptz_field= _name_here >=3D date_trunc( 'minute', d1.timestamptz_field_name_her= e )
where d1.timestamptz_field_name_here BETWEEN NOW() - interval '1 hou= r' and NOW()


>  


------=_Part_2086082_898197550.1618435983251--