Re: selecting records X minutes apart

From: Gavin Flower <GavinFlower(at)archidevsys(dot)co(dot)nz>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: selecting records X minutes apart
Date: 2011-06-12 21:50:16
Message-ID: 4DF53498.6000706@archidevsys.co.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

How about this (that does not require special functions nor triggers:

DROP TABLE IF EXISTS val;

CREATE TABLE val
(
id int,
ts timestamp
);

INSERT INTO val
VALUES
(0, '1-Jan-2010 20:00'),
(1, '1-Jan-2010 20:03'),
(1, '1-Jan-2010 20:04'),
(0, '1-Jan-2010 20:05'),
(1, '1-Jan-2010 20:05'),
(0, '1-Jan-2010 20:08'),
(1, '1-Jan-2010 20:09'),
(0, '1-Jan-2010 20:10');

WITH val_first AS
(
SELECT
id,
min(ts) AS ts
FROM
val
GROUP BY
id
)
SELECT
v.id,
v.ts::time
FROM
val v,
val_first vf
WHERE
v.id = vf.id AND
EXTRACT(EPOCH FROM v.ts - vf.ts)::int % 300 = 0
ORDER BY
id,
ts;

Browse pgsql-sql by date

  From Date Subject
Next Message Tarlika Elisabeth Schmitz 2011-06-13 19:04:54 extract some column/value pairs via hstore
Previous Message Andreas Kretschmer 2011-06-12 08:37:20 Re: Subselects not allowed?