0

我有以下给定日期范围的费率表。

我想编写一个 sql 查询(PostgreSQL)来获取给定时期的价格总和,如果它是一个连续时期..例如:

如果我在第一组中指定 2011-05-02 到 2011-05-09,则应返回 6 行的总和,

如果我在第二组中指定 2011-05-02 到 2011-05-011,则不应返回任何内容。

我的问题是我不知道如何确定日期范围是否连续......你能帮忙吗?非常感谢

案例1:预期总和

 price       from_date      to_date
------    ------------  ------------
   1.0      "2011-05-02"  "2011-05-02"
   2.0      "2011-05-03"  "2011-05-03"
   3.0      "2011-05-04"  "2011-05-05"
   4.0      "2011-05-05"  "2011-05-06"
   5.0      "2011-05-06"  "2011-05-07"
   4.0      "2011-05-08"  "2011-05-09"

案例 2:预期没有结果

 price       from_date      to_date
------    ------------  ------------
   1.0      "2011-05-02"  "2011-05-02"
   2.0      "2011-05-03"  "2011-05-03"
   3.0      "2011-05-07"  "2011-05-09"
   4.0      "2011-05-09"  "2011-05-011"

我没有重叠的费率日期范围。

4

3 回答 3

1

不确定我是否完全理解了这个问题,但是这个呢:

select * 
from prices
where not exists (
  select 1 from (
     select from_date - lag(to_date) over (partition by null order by from_date asc) as days_diff
     from prices
     where from_date >= DATE '2011-05-01' 
       and to_date < DATE '2011-07-01'
  ) t 
  where coalesce(days_diff, 0) > 1
) 
order by from_date
于 2011-05-05T13:03:57.927 回答
0

Here's a rather fonky way to solve it :

WITH RECURSIVE t AS (
  SELECT * FROM d WHERE '2011-05-02' BETWEEN start_date AND end_date 
  UNION ALL
  SELECT d.* FROM t JOIN d ON (d.key=t.key AND d.start_date=t.end_date+'1 DAY'::INTERVAL)  
     WHERE d.start_date <= '2011-05-09') 
  SELECT sum(price), min(start_date), max(end_date) 
  FROM t 
  HAVING min(start_date) <= '2011-05-02' AND max(end_date)>= '2011-05-09';
于 2011-05-05T13:26:44.120 回答
0

我认为您需要结合窗口函数和 CTE:

WITH
raw_rows AS (
SELECT your_table.*,
       lag(to_date) OVER w as prev_date,
       lead(from_date) OVER w as next_date
FROM your_table
WHERE ...
WINDOW w as (ORDER by from_date, to_date)
)
SELECT sum(stuff)
FROM raw_rows
HAVING bool_and(prev_date >= from_date - interval '1 day' AND
                next_date <= to_date + interval '1 day');

http://www.postgresql.org/docs/9.0/static/tutorial-window.html

http://www.postgresql.org/docs/9.0/static/queries-with.html

于 2011-05-08T12:39:08.980 回答