与窗口功能分组? Postgres

时间:2019-03-21 14:33:52

标签: postgresql function window

我有一个简短的问题

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

最后应该给我一个结果集,其中我还有一个附加列 这是每个“组”的唯一编号 该组从1/0开始,直到名称相同的最后0个条目

result

enter image description here

我可以通过窗口功能来实现吗 ? 我尝试了与其他的不同的density_rank和row_number等,但没有任何效果 谢谢

1 个答案:

答案 0 :(得分:0)

您没有指定在“开始”和“最后”之间发生什么排序,但是对于row_number()来说是可以的。如果需要,可以将其添加到以下解决方案的第一行。

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)

如果data_set中有一个主键,则可以从t3返回它,以用于最终联接。也就是说,

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