Re: Rows From but with Subqueries (or a cleaner non-array-using alternative)? - Mailing list pgsql-general

From Michael Lewis
Subject Re: Rows From but with Subqueries (or a cleaner non-array-using alternative)?
Date
Msg-id CAHOFxGqfT8vDAx0LtOv+osgmUnbFmH8XvhP2-PfBo8ZQENmDug@mail.gmail.com
Whole thread Raw
In response to Rows From but with Subqueries (or a cleaner non-array-using alternative)?  ("David G. Johnston" <david.g.johnston@gmail.com>)
List pgsql-general
In pseudo code, group_index is defined as:
case when 
LAG(v) OVER (ORDER BY i) = v then lag(i) ELSE i END, right?

If you have that in the first cte instead of the start/end business, then you can just select vals, group number, and row_num over that new grouping, right?

Something like this?


WITH vals (i,v) AS (VALUES (0,1),(1,0),(2,0),(3,1),(4,0),(5,0),(6,1),(7,1),(8,0),(9,1)),
grouped_vals AS (SELECT *,
case when LAG(v) OVER (ORDER BY i) = v then lag(i) OVER (ORDER BY i) ELSE i END AS group_index
FROM vals
)
select *, row_number() OVER (PARTITION BY group_index ORDER BY i)
from grouped_vals
where v = 0;

pgsql-general by date:

Previous
From: Michael Lewis
Date:
Subject: Re: increasing effective_cache_size slows down join queries by a factor of 4000x
Next
From: Shaozhong SHI
Date:
Subject: Turn a json column into a table