Re: How can I calculate differences between values

From: Andrei <andyk(at)commandprompt(dot)com>
To: A B <gentosaker(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How can I calculate differences between values
Date: 2009-11-10 16:06:16
Message-ID: 4AF98F78.9080600@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi;

also you can try this query which should work for any version

SELECT
a.userid, a.data, max(f.data), a.data - max(f.data)
FROM
(SELECT userid, data FROM foo EXCEPT SELECT userid, min(data) FROM
foo GROUP BY userid) a LEFT JOIN foo f ON (f.userid = a.userid AND
f.data < a.data) GROUP BY a.userid, a.data ORDER BY a.userid, a.data

A B wrote:
>> With 8.4's analytic capabilities you can do this:
>> select * from (
>> select userid, data - lag(data) over (partition by userid order by data)
>> diff
>> from foo) q
>> where diff is not null;
>>
>
> Thank you! That worked perfectly!
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Mead 2009-11-10 16:24:45 Re: Incremental Backups in postgres
Previous Message Thom Brown 2009-11-10 15:56:21 Re: -c argument not recognized