From: | Bob Singleton <bsingleton(at)ibss(dot)net> |
---|---|
To: | Rodrigo De León <rdeleonp(at)gmail(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: calculate time diffs across rows with single timestamp |
Date: | 2007-06-13 18:48:47 |
Message-ID: | 46703C0F.9080506@ibss.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Rodrigo De León wrote:
<blockquote
cite="mid1181753515(dot)419178(dot)63440(at)j4g2000prf(dot)googlegroups(dot)com"
type="cite">
<pre wrap="">On Jun 13, 11:17 am, <a class="moz-txt-link-abbreviated" href="mailto:bsingle(dot)(dot)(dot)(at)ibss(dot)net">bsingle(dot)(dot)(dot)(at)ibss(dot)net</a> (Bob Singleton) wrote:
</pre>
<blockquote type="cite">
<pre wrap="">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()}
</pre>
</blockquote>
<pre wrap=""><!---->
SELECT
TYPE, ID, STATUS
, (COALESCE(
(SELECT MIN(DATETIME) FROM STATUSLOG
WHERE 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 STATUSLOG
WHERE 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>
</body>
</html>
Attachment | Content-Type | Size |
---|---|---|
unknown_filename | text/html | 1.6 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Jyoti Seth | 2007-06-14 05:47:27 | Re: [SQL] setof or array as input parameter to postgresql 8.2 functions |
Previous Message | Rodrigo De León | 2007-06-13 16:51:55 | Re: calculate time diffs across rows with single timestamp |