pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tim Landscheidt <tim@tim-landscheidt.de>
To: pgsql-sql@postgresql.org
Subject: Re: grouping subsets
Date: Thu, 22 Jul 2010 11:31:23 +0000
Message-ID: <m3mxtju510.fsf@passepartout.tim-landscheidt.de> (raw)
References: <i28ubc$ie9$1@dough.gmane.org>
	<20100722100226.GE10348@a-kretschmer.de>
	<4C48230D.7030304@archonet.com>

Richard Huxton <dev@archonet.com> wrote:

>>> 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 ...

> It should be do-able in 8.4 onwards, look into windowing
> functions. In particular the lag() function:

> SELECT
>     mycode,
>     mydate,
>     lag(mycode) OVER (ORDER BY mydate) AS prev_code
> FROM
>     mytable
> ORDER BY mydate;

> It should be possible to use that as a subquery with an
> outer query that compares mycode=prev_code to get a run
> length.

Hmmm. Can the outer query be done without using "WITH
RECURSIVE"?

Tim




view thread (16+ messages)  latest in thread

Message-ID: <m3mxtju510.fsf@passepartout.tim-landscheidt.de>
Permalink:  ../m3mxtju510.fsf@passepartout.tim-landscheidt.de/
Also on:    postgresql.org/message-id/m3mxtju510.fsf@passepartout.tim-landscheidt.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tim@tim-landscheidt.de
  Subject: Re: grouping subsets
  In-Reply-To: <m3mxtju510.fsf@passepartout.tim-landscheidt.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox