Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 83B9D633CBA for ; Wed, 24 Mar 2010 11:08:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 69739-01 for ; Wed, 24 Mar 2010 14:08:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sargon.lncsa.com (mail.lncsa.com [213.215.28.11]) by mail.postgresql.org (Postfix) with ESMTP id 0BAC3633B56 for ; Wed, 24 Mar 2010 11:08:36 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 5462B3020C70 for ; Wed, 24 Mar 2010 15:08:33 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lncsa.com Received: from sargon.lncsa.com ([127.0.0.1]) by localhost (sargon.lncsa.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wOVivxYMr+da for ; Wed, 24 Mar 2010 15:08:33 +0100 (CET) Received: from zenon.apartia.fr (zenon.apartia.fr [10.0.3.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "zenon.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by sargon.lncsa.com (Postfix) with ESMTP id 335A8300DF00 for ; Wed, 24 Mar 2010 15:08:33 +0100 (CET) Received: from galba.apartia.fr (galba.apartia.fr [10.0.3.119]) by zenon.apartia.fr (Postfix) with ESMTP id D2FBC589ABE7C for ; Wed, 24 Mar 2010 15:08:32 +0100 (CET) Received: by galba.apartia.fr (Postfix, from userid 1000) id 971F41CC0B; Wed, 24 Mar 2010 15:08:32 +0100 (CET) Date: Wed, 24 Mar 2010 15:08:32 +0100 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: window function to sort times series data? Message-ID: <20100324140832.GA5864@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/171 X-Sequence-Number: 33878 Hi, I have time series data: price(id_price int, price int, created_on timestamp) I'd like to select the latest price before, say, 2010-03-10 and the latest price after that date. Using "group by" and self-joins I was able to build a (quite large :) working query. But I wonder if there is a cleaner, shorter solution with a window function. I tried something like: select * from (select first_value(p.id_price) over w as first_id_price, first_value(p.price) over w as first_price, first_value(p.created_on::date) over w as first_date, nth_value(p.id_price,2) over w as second_id_price, nth_value(p.price,2) over w as second_price, nth_value(p.created_on::date,2) over w as second_date, p.id_price from price p window w as (order by p.created_on > '2010-03-10, p.id_price desc rows between unbounded preceding and unbounded following)) as t where first_id_price=id_price; But this doesn't return correct results. Thanks for any suggestions,