Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 219E0632AA3 for ; Wed, 24 Mar 2010 11:36:53 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 01498-01 for ; Wed, 24 Mar 2010 14:36:33 +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 45C50632753 for ; Wed, 24 Mar 2010 11:36:42 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 642613022A47 for ; Wed, 24 Mar 2010 15:36:40 +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 wwcnyPdHgaWU for ; Wed, 24 Mar 2010 15:36:40 +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 181413020C70 for ; Wed, 24 Mar 2010 15:36:40 +0100 (CET) Received: from galba.apartia.fr (galba.apartia.fr [10.0.3.119]) by zenon.apartia.fr (Postfix) with ESMTP id B68925B93F308 for ; Wed, 24 Mar 2010 15:36:39 +0100 (CET) Received: by galba.apartia.fr (Postfix, from userid 1000) id A1E071CC0B; Wed, 24 Mar 2010 15:36:39 +0100 (CET) Date: Wed, 24 Mar 2010 15:36:39 +0100 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: window function to sort times series data? Message-ID: <20100324143639.GA7853@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20100324140832.GA5864@apartia.fr> <20100324142936.GB15442@a-kretschmer.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20100324142936.GB15442@a-kretschmer.de> User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.109 tagged_above=-10 required=5 tests=AWL=0.490, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/173 X-Sequence-Number: 33880 On Wed, Mar 24, 2010 at 03:29:36PM +0100, A. Kretschmer wrote: > In response to Louis-David Mitterrand : > > 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. > > test=*# select * from price ; > id_price | price | created_on > ----------+-------+--------------------- > 1 | 10 | 2010-01-01 00:00:00 > 1 | 12 | 2010-02-01 00:00:00 > 1 | 8 | 2010-03-01 00:00:00 > 1 | 15 | 2010-03-10 00:00:00 > 1 | 13 | 2010-03-20 00:00:00 > (5 rows) > > test=*# select * from ( > select distinct on(id_price) id_price, price, created_on from price where created_on < '2010-02-20'::date order by id_price, created_on desc > ) foo union all select * from ( > select distinct on(id_price) id_price, price, created_on from price where created_on > '2010-02-20'::date order by id_price, created_on asc > ) bar order by id_price,created_on ; > id_price | price | created_on > ----------+-------+--------------------- > 1 | 12 | 2010-02-01 00:00:00 > 1 | 8 | 2010-03-01 00:00:00 > (2 rows) > > That's okay for you? Yes, that works, but I forgot in my specs (!) that I'd like the two prices (pre and post 2010-03-10) to be returned on the same row and only if a post-2010-03-10 price exists. Thanks,