Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 2E2D51337B47 for ; Wed, 24 Nov 2010 16:52:19 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 43155-10 for ; Wed, 24 Nov 2010 20:52:12 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from zenon.apartia.fr (zenon.apartia.fr [82.66.93.83]) by mail.postgresql.org (Postfix) with ESMTP id D252F1337B34 for ; Wed, 24 Nov 2010 16:52:11 -0400 (AST) Date: Wed, 24 Nov 2010 21:53:16 +0100 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: "compressing" consecutive values into one Message-ID: <20101124205316.GA16666@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20101123151918.GA22094@apartia.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20101123151918.GA22094@apartia.fr> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.361 tagged_above=-5 required=5 tests=BAYES_00=-1.9, TO_NO_BRKTS_DIRECT=2.261 X-Spam-Level: X-Archive-Number: 201011/53 X-Sequence-Number: 34791 On Tue, Nov 23, 2010 at 04:19:18PM +0100, Louis-David Mitterrand wrote: > Hi, > > On time series price data I'm trying to remove consecutive identical > prices and keep only the latest. I tried: > > delete from price where id_price in (select t.id_price2 from (select > first_value(p.id_price) over w as id_price1, > nth_value(p.id_price, 2) over w as id_price2, > first_value(p.price) over w as price1, > nth_value(p.price,2) over w as price2 > from price p > window w as (partition by p.id_rate,p.id_cabin_category,p.id_cruise > order by p.id_price desc rows between unbounded preceding and > unbounded following)) as t where price1 = price2); > > and it mostly works but I have to do several runs to completely > eliminate identical consecutive prices. Actually I found the answer to my own question. It's the WINDOW lag/lead functions that I needed and this time one pass is enough: delete from price where id_price in ( select t.id_price1 from (select lead(p.id_price) over w as id_price1, lead(p.price) over w as price1, p.id_price, p.price from price p window w as (partition by p.id_rate,p.id_cabin_category,p.id_cruise order by p.id_price rows between unbounded preceding and unbounded following)) as t where t.price = t.price1);