Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id E72829FBC95 for ; Wed, 13 Jun 2007 13:29:03 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 47574-04-2 for ; Wed, 13 Jun 2007 13:28:48 -0300 (ADT) X-Greylist: delayed 00:18:50.765191 by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 0A0849FBCF8 for ; Wed, 13 Jun 2007 13:28:54 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from ganymede.ibss.net (66.49.75.50.nw.nuvox.net [66.49.75.50]) by svr4.postgresql.org (Postfix) with ESMTP id 651055AF89B for ; Wed, 13 Jun 2007 16:10:02 +0000 (GMT) Received: from [172.23.1.200] (jumper.ibss.net [172.23.1.200]) by ganymede.ibss.net (8.13.6/8.13.4) with ESMTP id l5DG9nHk000625 for ; Wed, 13 Jun 2007 12:09:49 -0400 Message-ID: <467018B0.30108@ibss.net> Date: Wed, 13 Jun 2007 12:17:52 -0400 From: Bob Singleton User-Agent: Thunderbird 1.5.0.10 (X11/20070302) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: calculate time diffs across rows with single timestamp Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV version 0.90.3, clamav-milter version 0.90.3 on ganymede.ibss.net X-Virus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50=0.001, FORGED_RCVD_HELO=0.135 X-Spam-Level: X-Archive-Number: 200706/93 X-Sequence-Number: 28583 First post - please pardon if I'm posted to the wrong group. I have a table 'statuslog' type varchar NOT NULL id varchar NOT NULL status varchar datetime timestamp NOT NULL Example data type id status datetime ASSET 001 AAA 2007-06-08 13:42:00.00 ASSET 002 AAA 2007-06-08 13:42:00.00 ASSET 003 AAA 2007-06-08 13:42:00.00 ASSET 001 BBB 2007-06-08 14:42:00.00 ASSET 001 CCC 2007-06-08 14:52:00.00 ASSET 002 BBB 2007-06-08 13:45:00.00 ASSET 001 DDD 2007-06-08 15:00:00.00 Consider this a log of transitional status changes. I now need to sumarize time-in-status with grouping on type, id, status. I can't currently modify the schema to include a second timestamp... I'm not (yet) well versed in temp tables and cursors, but from what I have researched and the suggestions from helpful coworkers, this seems the way to go...? Any suggestions on how I can build a result set that would return ASSET 001 AAA 1:00 (1 hour) ASSET 001 BBB 0:10 (10 minutes) ASSET 001 CCC 0:08 (8 minutes) ASSET 001 DDD {difference between timestamp and now()} ASSET 002 AAA 0:03 (3 minutes) ASSET 002 BBB {difference detween timestamp and now()} ASSET 003 AAA{diff between timestamp and now()} (The time diff can be seconds since epoch, some int, or whatever... in testing I set up the schema using a second timestamp (the 'in' stamp of the latter record by type/id became the 'out' stamp of the previous record) and I simply subtracted the in from the out time in a sum() with grouping.) Thanks, Bob