简体   繁体   English

PostgreSQL按间隔分组

[英]PostgreSQL group by with interval

Well, I have a seemingly simple set of data but it gives me a lot of trouble. 好吧,我有一组看似简单的数据,但这给我带来了很多麻烦。

This is an example of what my data look like: 这是我的数据的示例:

quantity  price1  price2 date
100       1       0      2018-01-01 10:00:00
200       1       0      2018-01-02 10:00:00
50        5       0      2018-01-02 11:00:00
100       1       1      2018-01-03 10:00:00
100       1       1      2018-01-03 11:00:00
300       1       0      2018-01-03 12:00:00

I need to sum up "quantity" column grouped by "price1" and "price2" and it would be very easy but I need to take into account time changes of "price1" and "price2". 我需要总结按“ price1”和“ price2”分组的“ quantity”列,这很容易,但是我需要考虑“ price1”和“ price2”的时间变化。 Data is sorted by "date". 数据按“日期”排序。

What I need is the last row to be not grouped with the first two although it has the same values for "price1" and "price2". 我需要的是最后一行不与前两个分组,尽管它的“ price1”和“ price2”具有相同的值。 Also I need to get minimal and maximal date of each interval. 我还需要获取每个间隔的最小和最大日期。

The end result should looks like this: 最终结果应如下所示:

quantity price1 price2 dateStart            dateEnd
300      1      0      2018-01-01 10:00:00  2018-01-02 10:00:00 
50       5      0      2018-01-02 11:00:00  2018-01-02 11:00:00
200      1      1      2018-01-03 10:00:00  2018-01-03 11:00:00
300      1      0      2018-01-03 12:00:00  2018-01-03 12:00:00

Any suggestions for a SQL query? 对SQL查询有什么建议吗?

It is a gap and island problem. 这是一个鸿沟和孤岛的问题。 Use the following code: 使用以下代码:

select sum(quantity), price1, price2, min(date) dateStart, max(date) dateend 
from
(
    select *,
           row_number() over (order by date) -
           row_number() over (partition by price1, price2 order by date) grp
    from data
) t
group by price1, price2, grp
order by dateStart

dbfiddle demo dbfiddle演示

The solution is based on an identification of consecutive sequences of price1 and price2 , which is done by a creation of the grp column. 该解决方案是基于连续序列的标识price1price2 ,这是由一个创建的完成grp柱。 Once you isolate the consecutive sequences then you do a simple group by using grp as well. 一旦分离出连续的序列,就可以使用grp进行简单的分组。

I changed a little bit the accepted answer to catch the cases when "date" column of two rows next to each other are exactly the same. 我稍稍更改了可接受的答案,以捕捉当相邻的两行的“日期”列完全相同时的情况。 I added second parameter so they will be ordered in correct order (my table has "oid" column) 我添加了第二个参数,因此它们将以正确的顺序排序(我的表具有“ oid”列)

select sum(quantity), price1, price2, min(date) dateStart, max(date) dateend 
from
(
    select *,
           row_number() over (order by date, oid) -
           row_number() over (partition by price1, price2 order by date, oid) grp
    from data
) t
group by price1, price2, grp
order by dateStart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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