簡體   English   中英

用窗口函數分組? Postgres的

[英]grouping with window functions? postgres

我有一個簡短的問題

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個條目

結果

在此輸入圖像描述

我可以通過窗口功能來實現嗎? 我嘗試了不同的dense_rank和row_number與先前等,但沒有任何工作謝謝

您沒有指定“開始”和“最后”之間發生了什么排序,但這對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有主鍵, data_set可以從t3返回它以在最終連接中使用。 那是,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM