简体   繁体   中英

grouping with window functions? postgres

i have a short question

select 
*
 from 
(
select 1 do_switch, 'abc', '2001-01-01'::TIMESTAMP 
union all
select 0 do_switch, 'xyz', '2001-01-01'::TIMESTAMP 
union all
select 1 do_switch, 'xyz', '2001-02-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-01-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-02-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-03-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-04-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-05-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-06-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-07-01'::TIMESTAMP 
union all
select 1 do_switch, 'bcd', '2001-08-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-09-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-10-01'::TIMESTAMP 
union all
select 0 do_switch, 'bcd', '2001-11-01'::TIMESTAMP 
union all
select 1 do_switch, 'bcd', '2001-12-01'::TIMESTAMP 
) data_set

it should give me at the end a resultset where i have an additional column which is a unique number per "group" the group start from 1/0 and goes till the last 0 entry for the same name

result

enter image description here

can I achieve this with a window function ? i tried with different dense_rank and row_number with preceeing etc but nothing worked thanks

You don't specify what ordering is happening between "start" and "last", but that's okay for row_number() . If needed, you can add that to the first line of the following solution.

with t1 as (select *, row_number() over (/*define order here*/) from data_set),
     t2 as (select row_number from t1 where do_switch = 1),
     t3 as (select row_number,
                   (
                     select min(t2.row_number)
                       from t2
                      where t2.row_number >= t1.row_number
                   )
              from t1
           )
 select t1.*, dense_rank() over (order by min) from t1 join t3 using (row_number);
 do_switch |   name   |      timestamp      | row_number | dense_rank 
-----------+----------+---------------------+------------+------------
         1 | abc      | 2001-01-01 00:00:00 |          1 |          1
         0 | xyz      | 2001-01-01 00:00:00 |          2 |          2
         1 | xyz      | 2001-02-01 00:00:00 |          3 |          2
         0 | bcd      | 2001-01-01 00:00:00 |          4 |          3
         0 | bcd      | 2001-02-01 00:00:00 |          5 |          3
         0 | bcd      | 2001-03-01 00:00:00 |          6 |          3
         0 | bcd      | 2001-04-01 00:00:00 |          7 |          3
         0 | bcd      | 2001-05-01 00:00:00 |          8 |          3
         0 | bcd      | 2001-06-01 00:00:00 |          9 |          3
         0 | bcd      | 2001-07-01 00:00:00 |         10 |          3
         1 | bcd      | 2001-08-01 00:00:00 |         11 |          3
         0 | bcd      | 2001-09-01 00:00:00 |         12 |          4
         0 | bcd      | 2001-10-01 00:00:00 |         13 |          4
         0 | bcd      | 2001-11-01 00:00:00 |         14 |          4
         1 | bcd      | 2001-12-01 00:00:00 |         15 |          4
(15 rows)

If there's a primary key in data_set , you can return it from t3 to use in the final join instead. That is,

with ...
     t2 as (select row_number ...),
     t3 as (select id, ...)
select data_set.*, dense_rank() ... join t3 using (id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM