2

我正在使用 PostgreSQL 8.3.8。

我在 time_boundaries 表中有一个时间边界列表(按日期):

CREATE TABLE role_times_boundaries
(
  role_date DATE,
  time_boundary TIME
);

INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-24'::date, '09:00:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-24'::date, '10:00:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '07:00:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '08:50:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '09:00:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '12:00:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '13:00:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '16:00:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '17:30:00'::time);
INSERT INTO role_times_boundaries (role_date, time_boundary) VALUES ('2013-04-25'::date, '20:00:00'::time);

所以,我有这个表格内容:

 role_date  | time_boundary 
------------+---------------
 2013-04-24 | 09:00:00
 2013-04-24 | 10:00:00
 2013-04-25 | 07:00:00
 2013-04-25 | 08:50:00
 2013-04-25 | 09:00:00
 2013-04-25 | 12:00:00
 2013-04-25 | 13:00:00
 2013-04-25 | 16:00:00
 2013-04-25 | 17:30:00
 2013-04-25 | 20:00:00

目标

我想通过将每个 time_boundary 作为“start_time”和下一个 time_boundary(按顺序)在同一日期对“role_times_boundaries”进行自我内连接来构建一个“时间片列表”表。目标是得到这样的结果:

 role_date  | start_time | end_time 
------------+------------+----------
 2013-04-24 | 09:00:00   | 10:00:00
 2013-04-25 | 07:00:00   | 08:50:00
 2013-04-25 | 08:50:00   | 09:00:00
 2013-04-25 | 09:00:00   | 12:00:00
 2013-04-25 | 12:00:00   | 13:00:00
 2013-04-25 | 13:00:00   | 16:00:00
 2013-04-25 | 16:00:00   | 17:30:00
 2013-04-25 | 17:30:00   | 20:00:00

暂定的

我试图通过这个 SQL 查询得到想要的结果

SELECT role_times_boundaries.role_date,
       role_times_boundaries.time_boundary AS start_time,
       end_time_boundaries.time_boundary AS end_time
FROM role_times_boundaries
INNER JOIN (
             SELECT role_date,
                    time_boundary
             FROM role_times_boundaries
           ) AS end_time_boundaries ON (
                                       role_times_boundaries.role_date = end_time_boundaries.role_date
                                       AND end_time_boundaries.time_boundary = (
                                                                                  SELECT MIN(a_list_of_end_boundaries.time_boundary)
                                                                                  FROM role_times_boundaries AS a_list_of_end_boundaries
                                                                                  WHERE a_list_of_end_boundaries.time_boundary > role_times_boundaries.time_boundary
                                                                                )
                                     )

结果如下:

 role_date  | start_time | end_time 
------------+------------+----------
 2013-04-24 | 09:00:00   | 10:00:00
 2013-04-25 | 07:00:00   | 08:50:00
 2013-04-25 | 08:50:00   | 09:00:00
 2013-04-25 | 12:00:00   | 13:00:00
 2013-04-25 | 13:00:00   | 16:00:00
 2013-04-25 | 16:00:00   | 17:30:00
 2013-04-25 | 17:30:00   | 20:00:00

如果你看得很清楚, 09:00: 00 到 12:00:00的时间片丢失了!但我仍然不明白为什么,仍然没有找到我的错误。

4

2 回答 2

3

如果您升级到 PostgreSQL 8.4 或更高版本,您可以使用窗口函数 (Oracle 术语中的“分析函数”),例如rank()row_number()lead()lag()

SELECT tb.role_date AS role_date
        , tb.time_boundary AS start_time
        , LEAD (time_boundary) OVER www AS end_time
FROM role_times_boundaries tb
WINDOW www AS (PARTITION BY tb.role_date ORDER BY tb.time_boundary)
        ;

或上述查询的另一个等价物:

SELECT tb.role_date AS role_date
        , tb.time_boundary AS start_time
        , LEAD (time_boundary) OVER ( PARTITION BY tb.role_date ORDER BY tb.time_boundary) AS end_time
FROM role_times_boundaries tb;

这将为您提供以下结果集:

 role_date  | start_time | end_time 
------------+------------+----------
 2013-04-24 | 09:00:00   | 10:00:00
 2013-04-24 | 10:00:00   | 
 2013-04-25 | 07:00:00   | 08:50:00
 2013-04-25 | 08:50:00   | 09:00:00
 2013-04-25 | 09:00:00   | 12:00:00
 2013-04-25 | 12:00:00   | 13:00:00
 2013-04-25 | 13:00:00   | 16:00:00
 2013-04-25 | 16:00:00   | 17:30:00
 2013-04-25 | 17:30:00   | 20:00:00
 2013-04-25 | 20:00:00   | 
(10 rows)

要删除没有 的句end_time点,您可以将其包装到子查询中:

SELECT role_date , start_time , end_time
FROM (
        SELECT tb.role_date AS role_date
        , tb.time_boundary AS start_time
        , LEAD (time_boundary) OVER ( PARTITION BY tb.role_date ORDER BY tb.time_boundary) AS end_time
        FROM role_times_boundaries tb
        ) sq
WHERE sq.start_time <= sq.end_time;

然后会给你以下结果:

 role_date  | start_time | end_time 
------------+------------+----------
 2013-04-24 | 09:00:00   | 10:00:00
 2013-04-25 | 07:00:00   | 08:50:00
 2013-04-25 | 08:50:00   | 09:00:00
 2013-04-25 | 09:00:00   | 12:00:00
 2013-04-25 | 12:00:00   | 13:00:00
 2013-04-25 | 13:00:00   | 16:00:00
 2013-04-25 | 16:00:00   | 17:30:00
 2013-04-25 | 17:30:00   | 20:00:00
(8 rows)

更新:另一个替代查询避免使用 WINDOW 函数,它通过使用NOT EXISTS关键字来解决问题:

SELECT lo.role_date
        , lo.time_boundary AS start_time
        , hi.time_boundary AS end_time
FROM role_times_boundaries lo
JOIN role_times_boundaries hi
    ON lo.role_date = hi.role_date
    AND lo.time_boundary < hi.time_boundary
    AND NOT EXISTS ( -- eliminate the men in the middle ...
        SELECT * FROM role_times_boundaries nx
        WHERE   nx.role_date = hi.role_date
        AND nx.time_boundary > lo.time_boundary
        AND nx.time_boundary < hi.time_boundary
        );
于 2013-04-27T15:38:40.067 回答
2

解决方案

好的,首先让我们稍微简化一下您的查询:

SELECT
  l.role_date,
  l.time_boundary AS start_time,
  r.time_boundary AS end_time
FROM role_times_boundaries l
INNER JOIN role_times_boundaries AS r ON ( -- You don't need that inner query, it's redundant
  l.role_date = r.role_date
  AND r.time_boundary = (
    SELECT MIN(r2.time_boundary)
    FROM role_times_boundaries AS r2
    WHERE r2.time_boundary > l.time_boundary))

现在的问题是您正在比较r2 中的所有 time_boundaries,而不是受角色日期限制的那些,因此更正的查询将是:

SELECT
  l.role_date,
  l.time_boundary AS start_time,
  r.time_boundary AS end_time
FROM role_times_boundaries l
INNER JOIN role_times_boundaries AS r ON (
  l.role_date = r.role_date
  AND r.time_boundary = (
    SELECT MIN(r2.time_boundary)
    FROM role_times_boundaries AS r2
    -- Note the added restriction:
    WHERE r2.time_boundary > l.time_boundary and r2.role_date = l.role_date))

备用查询

以下内容也适用于您的用例,并且可能更具可读性:

select
  l.role_date as role_date,
  l.time_boundary as start_time,
  min(r.time_boundary) as end_time
from role_times_boundaries l
join role_times_boundaries r on
  r.role_date = l.role_date
  and r.time_boundary > l.time_boundary
group by l.role_date, l.time_boundary
order by l.role_date, l.time_boundary
于 2013-04-27T15:04:31.857 回答