How to fetch values at regular hours?

From: Goran Hasse <gorhas(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: How to fetch values at regular hours?
Date: 2010-05-25 20:54:20
Message-ID: AANLkTilAR2NDe8zHnbBF8i6ul_D1x06Xhi6VKRD_vQjL@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello all!

I am logging my electricity meeter every five minute!

But the problem is that I would like to get values *just before* every hour
- like

select * from counter_log_view where name='CNT-3' and timestamp <
'2010-05-23 22:00:00' order by timestamp desc limit 1;
name | timestamp | count
-------+----------------------------+-------
CNT-3 | 2010-05-23 21:53:20.844422 | 69
(1 rad)

But I don't want to do a lot of selections and gather all the data in a
file.
Is there any better way to create the query so that I get the values just
before
every hour?

I would like to do;

freescada=> select * from counter_log_view where name='CNT-3' and timestamp
< '2010-05-23 18:00:00' order by timestamp desc limit 1;
name | timestamp | count
-------+---------------------------+-------
CNT-3 | 2010-05-23 17:53:18.58674 | 43
(1 rad)

freescada=> select * from counter_log_view where name='CNT-3' and timestamp
< '2010-05-23 19:00:00' order by timestamp desc limit 1;
name | timestamp | count
-------+----------------------------+-------
CNT-3 | 2010-05-23 18:53:19.151988 | 50
(1 rad)

freescada=> select * from counter_log_view where name='CNT-3' and timestamp
< '2010-05-23 20:00:00' order by timestamp desc limit 1;
name | timestamp | count
-------+----------------------------+-------
CNT-3 | 2010-05-23 19:53:19.683514 | 51
(1 rad)

In one query. Is this possible in *any* way?

--
gorhas(at)gmail(dot)com
Mob: 070-5530148

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John R Pierce 2010-05-25 21:05:37 Re: How to fetch values at regular hours?
Previous Message Justin Graf 2010-05-25 20:40:27 Re: Hiding data in postgresql