Re: Count of records in a row

From: Robert James <srobertjames(at)gmail(dot)com>
To: Elliot <yields(dot)falsehood(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Count of records in a row
Date: 2013-10-24 21:09:45
Message-ID: CAGYyBggK_V5yBOdGy82dk7dB1zYyANcO1SZr3eb1JF1vgLc8kg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/22/13, Elliot <yields(dot)falsehood(at)gmail(dot)com> wrote:
> It looks like you already found a solution, but here's one with a CTE. I
> cobbled this together from an older query I had for doing something
> similar, for which I unfortunately lost the original source of this
> approach. Also, this implies that there is something that gives an
> ordering to these rows (in this case, the field "i").
>
> create temp table data (i int, val char);
>
> insert into data (val, i)
> values
> ('A',1),
> ('A',2),
> ('A',3),
> ('B',4),
> ('C',5),
>
> with x
> as
> (
> select i,
> row_number() over () as xxx,
> val,
> row_number() over (partition by val order by i asc)
> - row_number() over () as d
> from data
> order by i
> )
> select val,
> count(*)
> from x
> group by d,
> val
> order by min(i)
> ;

Elliot - Thanks for this great solution; I've tested in on my data and
it gives great results.

I'd like to understand your code. I believe I understand most of it.
Can you explain what 'd' is?

And this clause "row_number() over (partition by val order by i asc) -
row_number() over () as d"?

(Hey, while I'm at it, is there a descriptive name for "x" too?)

Thanks

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2013-10-24 21:18:36 Re: Replication and fsync
Previous Message Jeff Janes 2013-10-24 20:51:09 Re: Replication and fsync