Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id EE3A29FB978 for ; Tue, 3 Jul 2007 15:49:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 39716-03-2 for ; Tue, 3 Jul 2007 15:49:36 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from web54305.mail.re2.yahoo.com (web54305.mail.re2.yahoo.com [206.190.49.115]) by postgresql.org (Postfix) with SMTP id 1A60B9FBBAC for ; Tue, 3 Jul 2007 15:49:35 -0300 (ADT) Received: (qmail 42648 invoked by uid 60001); 3 Jul 2007 18:49:35 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=3ovb/9ue5JIFsijVhJWhGKOefHC37yrNL4/jQHySBVhoDPftSeGwXTxf3d+gbwx7fte4Y8WPPNSmzxxpEQW0oNAOxC3UE4Lby/AvZshqjTyHqErP0ZeDjHWoqz3Rv68huQ/oNqm/szf3D/KJb91Zz60xx2rQBbUYxaONWpZhtcY=; X-YMail-OSG: 4CMKhr4VM1nnI1ZgNkcCau6wK0Gx5rKnkN.F0BzPEjyt.pssEDo6temQR0l5VfdJTEtCImKGyf8R7yiALp2cpB4QcP9oxsBl3EdpZWRzjEL4QRDG7VOaxxVhyyAPuroztqUfpu_zxG2FR61Vrpm9d4h6nw-- Received: from [75.41.74.118] by web54305.mail.re2.yahoo.com via HTTP; Tue, 03 Jul 2007 11:49:34 PDT Date: Tue, 3 Jul 2007 11:49:34 -0700 (PDT) From: chester c young Subject: Re: Iterate and write a previous row to a temp table? To: Bob Singleton Cc: sql pgsql In-Reply-To: <468A98D3.7030206@ibss.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <25291.42349.qm@web54305.mail.re2.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/24 X-Sequence-Number: 28759 --- 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. > > A colleague suggested a temp table, but I'm unsure how to iterate > until > I pass the startTime and then write the _previous_ and all subsequent > > rows to a temp table, stopping when I pass the endTime parameter. > > Any hints? > > Thanks! > Bob Singleton > couldn't you use the simple query: select * from sometable where timestamp between (select max(timestamp) from sometable where timestamp <= minTime) and maxTime ____________________________________________________________________________________ Finding fabulous fares is fun. Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains. http://farechase.yahoo.com/promo-generic-14795097