Thanks to visit codestin.com
Credit goes to postgrespro.com

Re: calculate time diffs across rows with single timestamp - Mailing list pgsql-sql

From Bob Singleton
Subject Re: calculate time diffs across rows with single timestamp
Date
Msg-id [email protected]
Whole thread Raw
In response to Re: calculate time diffs across rows with single timestamp  (Rodrigo De León <[email protected]>)
List pgsql-sql
Rodrigo De León wrote: <blockquote cite="[email protected]" type="cite"><pre
wrap="">OnJun 13, 11:17 am, <a class="moz-txt-link-abbreviated"
href="mailto:[email protected]">[email protected]</a>(Bob Singleton) wrote: </pre><blockquote type="cite"><pre
wrap="">Anysuggestions 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()}   </pre></blockquote><pre wrap="">
SELECT
TYPE, ID, STATUS
, (COALESCE((SELECT MIN(DATETIME) FROM STATUSLOGWHERE TYPE = S.TYPE AND ID = S.ID AND DATETIME > S.DATETIME),
NOW()::TIMESTAMP)- DATETIME) AS DURATION
 
FROM STATUSLOG S
ORDER BY TYPE, ID, STATUS


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
 </pre></blockquote> Awesome - thank you very much! Slightly modified to collapse by TYPE / ID / STATUS<br /><br /><pre
wrap="">SELECT
TYPE, ID, STATUS, SUM(   (COALESCE((SELECT MIN(DATETIME) FROM STATUSLOGWHERE TYPE = S.TYPE AND ID = S.ID AND DATETIME
>S.DATETIME), NOW()::TIMESTAMP) - DATETIME))
 
FROM STATUSLOG S
ORDER BY TYPE, ID, STATUS

Thanks for the lesson!

Bob Singleton
</pre>

pgsql-sql by date:

Previous
From: Rodrigo De León
Date:
Subject: Re: calculate time diffs across rows with single timestamp
Next
From: "Jyoti Seth"
Date:
Subject: Re: setof or array as input parameter to postgresql 8.2 functions