查询 PostgreSQL 9 中分组连续行的总和

Jür*_*üri 8 postgresql window-functions

我有关于在不同国家旅行的人的数据,如下所示:

country | begintimestamp      | distance    

Germany | 2015-01-01 00:00:00 | 100
Germany | 2015-01-01 01:12:13 | 30
France  | 2015-01-01 02:13:14 | 40
France  | 2015-01-01 03:14:15 | 20
Spain   | 2015-01-01 04:15:16 | 10
France  | 2015-01-01 05:16:17 | 30
France  | 2015-01-01 05:17:18 | 5
Germany | 2015-01-01 06:18:19 | 3
Run Code Online (Sandbox Code Playgroud)

我需要的是能够收到这样的结果 -distance与最早的连续行相加begintimestamp

country | begintimestamp      | distance

Germany | 2015-01-01 00:00:00 | 130  // 100+30, the distance of two first rows summed.
France  | 2015-01-01 02:13:14 | 60   // 40+20
Spain   | 2015-01-01 04:15:16 | 10   // 
France  | 2015-01-01 05:16:17 | 35   // 30+5
Germany | 2015-01-01 06:18:19 | 3
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用 PG 窗口函数,但无法想出任何能让我更接近结果的东西。

a_h*_*ame 13

select min(country) as country,
       min(begintimestamp) as first_begin_ts, 
       sum(distance) as distance
from (
  select t1.*, 
         sum(group_flag) over (order by begintimestamp) as grp
  from (
      select *,
             case
                when lag(country) over (order by begintimestamp) = country then null
                else 1
              end as group_flag
      from travel
  ) t1
) t2
group by grp
order by first_begin_ts;
Run Code Online (Sandbox Code Playgroud)

t1每次国家/地区更改时,最内部的查询(别名)都会创建一个数字)。然后,第二级查询(别名t2)对这些标志进行运行求和,这实质上为每个连续的国家/地区集提供了不同的数字。最外面的查询然后按该数字分组并对距离求和。这min(country)对于使group by操作员满意是必要的,但是由于所有具有相同的行grp无论如何都具有相同的国家/地区,因此无关紧要。

SQLFiddle:http ://sqlfiddle.com/#!15/fe341/1