Received: from localhost (pluto.hub.org [200.46.204.4]) by postgresql.org (Postfix) with ESMTP id A36819FA333 for ; Sun, 5 Nov 2006 06:50:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.4]) (amavisd-new, port 10024) with ESMTP id 84885-01 for ; Sun, 5 Nov 2006 06:50:30 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from 137086.vserver.de (vs137086.vserver.de [62.75.137.86]) by postgresql.org (Postfix) with ESMTP id 584F59FA2CC for ; Sun, 5 Nov 2006 06:50:33 -0400 (AST) Received: from a-kretschmer.de (p54B3D0BD.dip0.t-ipconnect.de [84.179.208.189]) (authenticated bits=0) by 137086.vserver.de (8.12.8/8.12.8) with ESMTP id kA5AnCRv023573 for ; Sun, 5 Nov 2006 11:49:14 +0100 Received: from kretschmer by a-kretschmer.de with local (Exim 4.61) (envelope-from ) id 1GgfaK-0004Pi-4Y for pgsql-general@postgresql.org; Sun, 05 Nov 2006 11:50:40 +0100 Date: Sun, 5 Nov 2006 11:50:40 +0100 From: Andreas Kretschmer To: pgsql-general@postgresql.org Subject: Re: help with a query Message-ID: <20061105105040.GB8761@KanotixBox> References: <004b01c6ff86$bc254ee0$23d6bed5@oem41cbbf9e178> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <004b01c6ff86$bc254ee0$23d6bed5@oem41cbbf9e178> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: mutt-ng 1.5.9i (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/260 X-Sequence-Number: 103356 Pedro Doria Meunier schrieb: > > Hi all! > > This is most certainly a lame question but perhaps someone is gracious enough > to lend me a hand& ;-) > > I have the following setup in a table: > > The first record which is to be found (ok easy enough :D) with a timestamp > meets a certain condition (ignition off) > The following record is for the event of ignition on again with a timestamp. > > So the question here is: how can I compute the time difference between these > two records in a single query? > Better yet: finding all the records that meet the first condition (ignition > off) and the immediately following records as to compute the time difference. > ;-) Okay, let me try. First, i created a similar table: test=# select * from test; id | nr | ts | event ----+----+----------------------------+------- 1 | 1 | 2006-11-05 11:20:34.308945 | 0 2 | 2 | 2006-11-05 11:20:41.245691 | 0 3 | 2 | 2006-11-05 11:20:43.630381 | 1 4 | 1 | 2006-11-05 11:20:49.762882 | 1 5 | 3 | 2006-11-05 11:20:55.427288 | 0 (5 rows) As we can see, i have a column nr to identify paired rows. The event-column is similar to your ignition (off-on -> 0-1). The rows with id 1 and 4, and 2 and 3 paired. There are only 0-events and paired 1-events or only a 0-event, and only one pair for every nr. Now i want to know the elapsed time for every nr (1 and 2) between the 0 and 1 - event: test=# select a.id, a.nr, a.ts as event_off, a.event, b.id, b.ts as event_on, b.ts-a.ts as elapsed from test a, test b where (a.nr=b.nr and a.ts