Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL group by,带间隔但不带窗口函数_Sql_Postgresql_Gaps And Islands - Fatal编程技术网

PostgreSQL group by,带间隔但不带窗口函数

PostgreSQL group by,带间隔但不带窗口函数,sql,postgresql,gaps-and-islands,Sql,Postgresql,Gaps And Islands,这是我上一个问题的后续问题: 有一个非常好的答案,但不幸的是,它不适用于PostgreSQL 8.0-一些客户端仍然使用这个旧版本 所以我需要找到另一个不使用窗口函数的解决方案 以下是我的桌子: id quantity price1 price2 date 1 100 1 0 2018-01-01 10:00:00 2 200 1 0 2018-01-02 10:00:00 3 50

这是我上一个问题的后续问题:

有一个非常好的答案,但不幸的是,它不适用于PostgreSQL 8.0-一些客户端仍然使用这个旧版本

所以我需要找到另一个不使用窗口函数的解决方案

以下是我的桌子:

   id quantity  price1  price2 date
    1  100       1       0      2018-01-01 10:00:00
    2  200       1       0      2018-01-02 10:00:00
    3  50        5       0      2018-01-02 11:00:00
    4  100       1       1      2018-01-03 10:00:00
    5  100       1       1      2018-01-03 11:00:00
    6  300       1       0      2018-01-03 12:00:00
我需要对价格1和价格2分组的数量求和,但仅当它们发生变化时

所以最终结果应该是这样的:

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

虽然效率不高,但您可以通过子查询实现相同的逻辑:

select sum(quantity), price1, price2,
       min(date) as dateStart, max(date) as dateend 
from (select d.*,
             (select count(*)
              from data d2
              where d2.date <= d.date
             ) as seqnum,
             (select count(*)
              from data d2
              where d2.price1 = d.price1 and d2.price2 = d.price2 and d2.date <= d.date
             ) as seqnum_pp
      from data d
     ) t
group by price1, price2, (seqnum - seqnum_pp)
order by dateStart

虽然效率不高,但您可以通过子查询实现相同的逻辑:

select sum(quantity), price1, price2,
       min(date) as dateStart, max(date) as dateend 
from (select d.*,
             (select count(*)
              from data d2
              where d2.date <= d.date
             ) as seqnum,
             (select count(*)
              from data d2
              where d2.price1 = d.price1 and d2.price2 = d.price2 and d2.date <= d.date
             ) as seqnum_pp
      from data d
     ) t
group by price1, price2, (seqnum - seqnum_pp)
order by dateStart

非常感谢。同时,我使用左连接得到了类似的解决方案。我会把它贴出来,这样人们可以比较。测试将显示哪个更快。看起来您的查询工作正常。至少如果我在一小部分数据上测试它。但是如果我在真实的数据库上运行它,现在已经超过15分钟了,仍然没有结果…@GeorgiBonchev。你的客户应该更新他们的软件。性能问题已解决。谢谢!同时,我使用左连接得到了类似的解决方案。我会把它贴出来,这样人们可以比较。测试将显示哪个更快。看起来您的查询工作正常。至少如果我在一小部分数据上测试它。但是如果我在真实的数据库上运行它,现在已经超过15分钟了,仍然没有结果…@GeorgiBonchev。你的客户应该更新他们的软件。性能问题是一个固定的问题。