Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 80EAE632824 for ; Wed, 24 Mar 2010 12:04:54 -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 79070-03 for ; Wed, 24 Mar 2010 15:04:44 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.8]) by mail.postgresql.org (Postfix) with ESMTP id 980636322EB for ; Wed, 24 Mar 2010 12:04:43 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0MOlGA-1NzQDf34L8-005Vnx; Wed, 24 Mar 2010 16:04:42 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1NuS8P-00062a-00 for pgsql-sql@postgresql.org; Wed, 24 Mar 2010 16:04:41 +0100 Date: Wed, 24 Mar 2010 16:04:41 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: window function to sort times series data? Message-ID: <20100324150441.GC15442@a-kretschmer.de> References: <20100324140832.GA5864@apartia.fr> <20100324142936.GB15442@a-kretschmer.de> <20100324143639.GA7853@apartia.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20100324143639.GA7853@apartia.fr> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX18lLsx2FTmviffCI30Dp0hh3/eC3/Zp2MeyprE iTDLyBqFmpJh2sgpIb7CqJmrsc6mp0mw+RZ4NaMcuZEik+5EMP qXXXx/vDLBloLsKHf6WEQ== 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/174 X-Sequence-Number: 33881 In response to Louis-David Mitterrand : > 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. Well: 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 id_price, sum(case when pre_post='pre' then price else 0 end) as pre, sum(case when pre_post='post' then price else 0 end) as post, max(case when pre_post='pre' then created_on else null end) as date_pre, max(case when pre_post='post' then created_on else null end) as date_post from (select * from (select distinct on(id_price) 'pre'::text as pre_post, 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) 'post'::text, 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) foo group by id_price; id_price | pre | post | date_pre | date_post ----------+-----+------+---------------------+--------------------- 1 | 12 | 8 | 2010-02-01 00:00:00 | 2010-03-01 00:00:00 (1 row) You can filter that result for rows where date_post is not null. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99