Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 216246339E6 for ; Tue, 27 Jul 2010 07:38:01 -0300 (ADT) 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 08951-07 for ; Tue, 27 Jul 2010 10:37:53 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from jupiter.marktest.pt (delta.marktest.pt [80.251.174.116]) by mail.postgresql.org (Postfix) with ESMTP id C5DED6329E1 for ; Tue, 27 Jul 2010 07:37:53 -0300 (ADT) Received: from Proteu (proteu.marktest.pt [10.61.90.54]) by jupiter.marktest.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id PBK1KGL3; Tue, 27 Jul 2010 11:37:50 +0100 Message-ID: <61EBCE3AA9564FFDB88B18177F726FCB@marktestcr.marktest.pt> From: "Oliveiros d'Azevedo Cristina" To: , "Rainer Stengele" References: Subject: Re: grouping subsets Date: Tue, 27 Jul 2010 11:37:49 +0100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="ISO-8859-15"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.229 tagged_above=-5 required=5 tests=BAYES_50=0.8, STOX_REPLY_TYPE=0.439, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: * X-Archive-Number: 201007/106 X-Sequence-Number: 34419 Howdy, Rainer. It's been a while, so I don't know if you are still interested in this problem or if you, in the meantime, found yourself a solution, but I've tried this on a local copy of the example you provided and it seems to work. The problem is that I suspect that if you have several thousands of records on your table it will become slow... Best, Oliveiros SELECT SUM(tudo.parcela),tudo.a FROM ( SELECT fo.parcela,fo.a,fo.b,fo.c,MIN(th.c) as d FROM ( SELECT se.a as parcela,se.b as a,se.c as b, MAX(pr.c) as c FROM yourTable se LEFT JOIN ( SELECT a.* FROM yourTable a JOIN yourTable b ON (b.b <> a.b) AND ((age(a.c,b.c) = '1 day'::interval) ) ) pr ON pr.b = se.b AND se.c >= pr.c GROUP BY se.a,se.b,se.c ) fo LEFT JOIN ( SELECT a.* FROM yourTable a JOIN yourTable b ON (b.b <> a.b) AND ((age(a.c,b.c) = '-1 day'::interval) ) ) th ON fo.a = th.b AND fo.b <= th.c GROUP BY fo.parcela,fo.a,fo.b,fo.c ) tudo GROUP BY tudo.a,tudo.c,tudo.d To: Sent: Thursday, July 22, 2010 9:09 AM Subject: [SQL] grouping subsets > Hi, > > having a table similar to > > | 1 | B | [2010-07-15 Do] | > | 1 | B | [2010-07-16 Fr] | > |---+---+-----------------| > | 2 | C | [2010-07-17 Sa] | > | 2 | C | [2010-07-18 So] | > |---+---+-----------------| > | 1 | B | [2010-07-19 Mo] | > | 1 | B | [2010-07-20 Di] | > | 1 | B | [2010-07-21 Mi] | > | 1 | B | [2010-07-22 Do] | > |---+---+-----------------| > | 3 | D | [2010-07-23 Fr] | > > a simple group by gives me: > > | 6 | B | > | 4 | C | > | 3 | D | > > > What I want to get is the values grouped by "subset", where a subset is a > set of rows with identical column until the colum changes. > Is there a way to get > > | 2 | B | > | 4 | C | > | 4 | B | > | 3 | D | > > by SQL only? > > - Rainer > > > > > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql