Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 6B52B6349EA for ; Thu, 22 Jul 2010 07:02:36 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 98883-01 for ; Thu, 22 Jul 2010 10:02:27 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.9]) by mail.postgresql.org (Postfix) with ESMTP id B22206346E1 for ; Thu, 22 Jul 2010 07:02:28 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap1) with ESMTP (Nemesis) id 0Lo3Ka-1PDPHW49yC-00gYsx; Thu, 22 Jul 2010 12:02:27 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1Obsbi-0008Dc-00 for pgsql-sql@postgresql.org; Thu, 22 Jul 2010 12:02:26 +0200 Date: Thu, 22 Jul 2010 12:02:26 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: grouping subsets Message-ID: <20100722100226.GE10348@a-kretschmer.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V02:K0:QZdJI6/EKWb9reBueI4xVTwK/3ZkZ/OlclHUjCGjKmQ gg+PBO+j6dfJjXK1FJiZhtsaEwb6B8MXAWNAUp8xUYu2t/S9oZ RfWunE5pHoHhRTAUQwM5coQjSMxC8U/X9c/YH9dYxXDGh8cVPp 9mr+ybz9oc1MUX+567XRrB2zxERe602BGfvsaNUJW5WkGuj16k 0uXaC8FLr+6ExolqpBV5A== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.81 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001, T_FRT_CONTACT=0.01 X-Spam-Level: X-Archive-Number: 201007/90 X-Sequence-Number: 34403 In response to Rainer Stengele : > 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? I think, the problem is that there are 2 identical groups. I think, you can write a pl/pgsql-proc, selecting all ordered by the date-field and walking through the result to do the grouping, checking if the 2nd column is different from the previous. With plain SQL it's maybe possible too, but i don't know how ... Regards, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99