SQL / Postgres日期时间划分/规范化

时间:2014-12-10 10:41:53

标签: sql database postgresql datetime views

我有这个活动表

+--------------+------------------+
| Field        | Type             |
+--------------+------------------+
| id           | int(11) unsigned |
| start_date   | timestamp        |
| end_date     | timestamp        |
| ...          |                  |
+--------------+------------------+

我需要一个视图,它按照DAY的start_date对这些活动进行分组,但是这样的方式是,如果end_date与start_date不在同一天,则视图会再次包含该条目,但start_date设置为00:00第二天..(等等,根据需要重复多次,直到start_date与end_date在同一天)

举个例子:

如果活动表包含:

+--------------+----------------------------+----------------------------+
| id           | start_date                 | end_date                   |
+--------------+----------------------------+----------------------------+
|  1           | 2014-12-02 14:12:00+00     | 2014-12-03 06:45:00+00     |   
|  2           | 2014-12-05 15:25:00+00     | 2014-12-05 07:29:00+00     |                                
+--------------+----------------------------+----------------------------+

视图应包含:

+--------------+----------------------------+----------------------------+
| activity_id  | start_date                 | end_date                   |
+--------------+----------------------------+----------------------------+
|  1           | 2014-12-02 14:12:00+00     | 2014-12-02 23:59:59+00     |   
|  1           | 2014-12-03 00:00:00+00     | 2014-12-03 06:45:00+00     |
|  2           | 2014-12-05 15:25:00+00     | 2014-12-05 07:29:00+00     |                                  
+--------------+----------------------------+----------------------------+

非常感谢任何帮助!

PS:我正在使用postgresql

1 个答案:

答案 0 :(得分:1)

要获取所需的行,请先使用set returning functionlateral join。从那里开始,使用CASE语句和date arithmetics来提取相关值。

这是一个让你入门的例子:

with data as (
  select id, start_date, end_date
  from (values
    (1, '2014-12-02 14:12:00+00'::timestamptz, '2014-12-03 06:45:00+00'::timestamptz),
    (2, '2014-12-05 15:25:00+00'::timestamptz, '2014-12-05 07:29:00+00'::timestamptz)
  ) as rows (id, start_date, end_date)
)
select data.id,
      case days.d = date_trunc('day', data.start_date)
        when true then data.start_date
        else days.d
      end as start_date,
      case days.d = date_trunc('day', data.end_date)
        when true then data.end_date
        else days.d + interval '1 day' - interval '1 sec'
      end as end_date
from data
join generate_series(
      date_trunc('day', data.start_date),
      date_trunc('day', data.end_date),
      '1 day'
      ) as days (d)
      on days.d >= date_trunc('day', data.start_date)
      and days.d <= date_trunc('day', data.end_date)

 id |       start_date       |        end_date        
----+------------------------+------------------------
  1 | 2014-12-02 15:12:00+01 | 2014-12-02 23:59:59+01
  1 | 2014-12-03 00:00:00+01 | 2014-12-03 07:45:00+01
  2 | 2014-12-05 16:25:00+01 | 2014-12-05 08:29:00+01
(3 rows)

顺便说一下,根据您正在做的事情,使用date range可能更有意义:

with data as (
  select id, start_date, end_date
  from (values
    (1, '2014-12-02 14:12:00+00'::timestamptz, '2014-12-03 06:45:00+00'::timestamptz),
    (2, '2014-12-05 07:25:00+00'::timestamptz, '2014-12-05 15:29:00+00'::timestamptz)
  ) as rows (id, start_date, end_date)
)
select data.id,
      tstzrange(data.start_date, data.end_date)
from data;

 id |                      tstzrange                      
----+-----------------------------------------------------
  1 | ["2014-12-02 15:12:00+01","2014-12-03 07:45:00+01")
  2 | ["2014-12-05 08:25:00+01","2014-12-05 16:29:00+01")
(2 rows)