Re: How can I calculate differences between values - Mailing list pgsql-general

From Andrei
Subject Re: How can I calculate differences between values
Date
Msg-id 4AF98F78.9080600@commandprompt.com
Whole thread Raw
In response to Re: How can I calculate differences between values  (A B <gentosaker@gmail.com>)
List 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!
 

pgsql-general by date:

Previous
From: Thom Brown
Date:
Subject: Re: -c argument not recognized
Next
From: Albe Laurenz
Date:
Subject: Re: PostgreSQL 8.3.8 on AIX5.3 : compilation failed