Re: Count of records in a row
От | Robert James |
---|---|
Тема | Re: Count of records in a row |
Дата | |
Msg-id | CAGYyBggK_V5yBOdGy82dk7dB1zYyANcO1SZr3eb1JF1vgLc8kg@mail.gmail.com обсуждение исходный текст |
Ответ на | Re: Count of records in a row (Elliot <yields.falsehood@gmail.com>) |
Ответы |
Re: Count of records in a row
|
Список | pgsql-general |
On 10/22/13, Elliot <yields.falsehood@gmail.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
В списке pgsql-general по дате отправления: