SQL/Postgres 日期时间划分/规范化
Posted
技术标签:
【中文标题】SQL/Postgres 日期时间划分/规范化【英文标题】:SQL/Postgres datetime division / normalizing 【发布时间】:2014-12-10 10:41:52 【问题描述】:我有这个活动表
+--------------+------------------+
| Field | Type |
+--------------+------------------+
| id | int(11) unsigned |
| start_date | timestamp |
| end_date | timestamp |
| ... | |
+--------------+------------------+
我需要一个视图,该视图按 start_date 按 DAY 对这些活动进行分组,但如果 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】:要获取所需的行,首先使用set returning function 和lateral 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)
【讨论】:
以上是关于SQL/Postgres 日期时间划分/规范化的主要内容,如果未能解决你的问题,请参考以下文章
PFQueryTableViewController 快速按日期划分