Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 162129FA0FB for ; Fri, 15 Dec 2006 09:21:49 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 39720-10 for ; Fri, 15 Dec 2006 09:21:33 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.180]) by postgresql.org (Postfix) with ESMTP id 4CA129F9B05 for ; Fri, 15 Dec 2006 09:21:38 -0400 (AST) Received: by py-out-1112.google.com with SMTP id a73so421702pye for ; Fri, 15 Dec 2006 05:21:37 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=GfNjF+vwYgDH25kIj7ZuqYT7Y8ZvO3tUvKbTImj7I9uN8EIxYkP33jNl4xU5lVa90C54pNZSFVIz3Op4t5X9hluFVtK7z0hz3kAK0mYgUrPpgoDME2wQDFZZ9dJtAjYqFvyPDHkuo6gbEnXN4qlbgZHC+z49N291IrzHEfQ+QUs= Received: by 10.35.26.14 with SMTP id d14mr1463940pyj.1166188896870; Fri, 15 Dec 2006 05:21:36 -0800 (PST) Received: by 10.35.131.17 with HTTP; Fri, 15 Dec 2006 05:21:36 -0800 (PST) Message-ID: <6bdfe880612150521n137d80f7q463955f26db1cef0@mail.gmail.com> Date: Fri, 15 Dec 2006 19:21:36 +0600 From: "Partha Guha Roy" To: "Richard Broersma Jr" Subject: Re: Need to subtract values between previous and current row Cc: pgsql-sql@postgresql.org In-Reply-To: <297264.3730.qm@web31806.mail.mud.yahoo.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_125288_11245391.1166188896817" References: <6bdfe880612150427o19bfe90ch87a972c0f5898ffb@mail.gmail.com> <297264.3730.qm@web31806.mail.mud.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/114 X-Sequence-Number: 27070 ------=_Part_125288_11245391.1166188896817 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, Thanks for everyones email. Let me clarify a little bit more. Actually there is a state change of an entity. now the results are stored in the following manner: CID ATIME STATE 101 12/10/2006 1 101 12/12/2006 2 101 12/14/2006 1 101 12/17/2006 2 102 12/14/2006 1 102 12/16/2006 2 102 12/18/2006 3 Now I have to find which CID stayed on which STATE for how long. Lets assume today is the 20th. So the result would be: CID DURATION STATE 101 2 1 101 2 2 101 3 1 101 3 2 102 2 1 102 2 2 102 2 3 The constraints are: I can't use any plpgsql or other function. There can be gaps in CID. No sequence can be created. Thanks. On 12/15/06, Richard Broersma Jr wrote: > > > > > ID ATIME (MM/dd/yyyy) > > == ==== > > 1 10/12/2006 > > 2 10/14/2006 > > 3 10/18/2006 > > 4 10/22/2006 > > 5 10/30/2006 > > > > Now I need a query that will subtract atime of row 1 from row 2, row2 > from > > row3 and so on... > > just an idea. > > select (A.atime - max(B.atime)) duration > from table A join table B > on (A.atime > B.atime) > group by A.atime; > > Regards, > Richard Broersma Jr. > -- ------------------------------ Partha Guha Roy *********************************************** Take everything you like seriously, except yourselves. - Rudyard Kipling ------=_Part_125288_11245391.1166188896817 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi,

Thanks for everyones email. Let me clarify a little bit more.

Actually there is a state change of an entity. now the results are stored in the following manner:

CID        ATIME          STATE
101        12/10/2006     1
101        12/12/2006     2
101        12/14/2006     1
101        12/17/2006     2
102        12/14/2006     1
102        12/16/2006     2
102        12/18/2006     3

Now I have to find which CID stayed on which STATE for how long.

Lets assume today is the 20th.
So the result would be:

CID      DURATION      STATE
101      2                     1
101      2                     2
101      3                     1
101      3                      2
102      2                     1
102      2                     2
102      2                     3

The constraints are:
I can't use any plpgsql or other function.
There can be gaps in CID.
No sequence can be created.

Thanks.


On 12/15/06, Richard Broersma Jr <rabroersma@yahoo.com> wrote:
>
> ID           ATIME (MM/dd/yyyy)
> ==          ====
> 1            10/12/2006
> 2            10/14/2006
> 3            10/18/2006
> 4            10/22/2006
> 5            10/30/2006
>
> Now I need a query that will subtract atime of row 1 from row 2, row2 from
> row3  and so on...

just an idea.

select (A.atime - max(B.atime)) duration
from table A join table B
on ( A.atime > B.atime)
group by A.atime;

Regards,
Richard Broersma Jr.



--
------------------------------
Partha Guha Roy
***********************************************
Take everything you like seriously, except yourselves.
- Rudyard Kipling ------=_Part_125288_11245391.1166188896817--