Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id DC1A29FBE68 for ; Tue, 3 Jul 2007 15:57:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 49677-07 for ; Tue, 3 Jul 2007 15:57:22 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 7F86F9FBD27 for ; Tue, 3 Jul 2007 15:57:22 -0300 (ADT) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 4B41E1434EB; Tue, 3 Jul 2007 19:57:21 +0100 (BST) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 273FF27410E; Tue, 3 Jul 2007 19:57:21 +0100 (BST) Message-ID: <468A9C10.8070005@archonet.com> Date: Tue, 03 Jul 2007 19:57:20 +0100 From: Richard Huxton User-Agent: Thunderbird 2.0.0.0 (X11/20070326) MIME-Version: 1.0 To: Bob Singleton Cc: pgsql-sql@postgresql.org Subject: Re: Iterate and write a previous row to a temp table? References: <468A98D3.7030206@ibss.net> In-Reply-To: <468A98D3.7030206@ibss.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/25 X-Sequence-Number: 28760 Bob Singleton wrote: > Revisiting a Time In Status query I received help on - I'm trying to > narrow down a subset of data I return for analysis. > > Given a statusLog as entityId, statusId, timestamp that might look > something like > > entityId | statusId | timestamp > -------------------------------------------- > 001 | HLD | 2007-06-14 11:07:35.93 > 001 | RDY | 2007-06-15 11:07:35.93 > 001 | USE | 2007-06-16 11:07:35.93 > 001 | RDY | 2007-06-17 11:07:35.93 > 001 | MNT | 2007-06-18 11:07:35.93 > > I need to pull for a given span of time - say 2007-06-16 00:00:00.01 > (let me call it startTime) to 2007-06-17 23:59:59.99 (call it endTime) > in such a way that rows with a timestamp between startTime and endTime > AND the latest record prior to or equal to startTime are returned. In > the above simplified example, only the second and third rows would be > returned. Can't be done, because you don't have a primary key, so no way to distinguish between duplicate rows. However, if you just eliminate duplicates you could just use a function like (not tested): CREATE FUNCTION ranged( startTime timestamp with time zone, endTime timestamp with time zone, ) RETURNS SETOF statusLog AS $$ SELECT entityid,statusid,timestamp FROM statusLog WHERE timestamp BETWEEN startTime AND endTime UNION SELECT entityid,statusid,timestamp FROM statusLog WHERE timestamp <= startTime ORDER BY timestamp DESC LIMIT 1 ORDER BY $$ LANGUAGE SQL; Note that UNION eliminates duplicates, if you want to keep them use "UNION ALL" HTH -- Richard Huxton Archonet Ltd