SQL 显示重叠的时间段
SQL to show overlapping time periods
如何检查 Postgresql 9.2(SQL 命令),如果在时间戳记录中有一些时间段与来自相同 id_user 的其他时间段重叠。我需要更正现有的 table.
例如,查询显示第 1,3 和 4 行。
id | id_user | timedate0 | timedate2
---------------------------------------------------
1 | 1 | 2020-04-20 12:00:00 | 2020-04-20 14:00:00
2 | 1 | 2020-04-20 17:00:00 | 2020-04-20 19:30:00
3 | 1 | 2020-04-20 14:30:00 | 2020-04-20 15:40:00
4 | 1 | 2020-04-20 13:00:00 | 2020-04-20 15:00:00
5 | 1 | 2020-04-21 13:00:00 | 2020-04-21 14:00:00
6 | 1 | 2020-04-21 14:00:00 | 2020-04-21 15:00:00
您可以使用 exists
:
select t.*
from t
where exists (select 1
from t t2
where t2.timedate0 < t.timedate2 and
t2.timedate2 > t.timedate0 and
t2.id_user = t.id_user and t2.id <> t.id
);
如何检查 Postgresql 9.2(SQL 命令),如果在时间戳记录中有一些时间段与来自相同 id_user 的其他时间段重叠。我需要更正现有的 table.
例如,查询显示第 1,3 和 4 行。
id | id_user | timedate0 | timedate2
---------------------------------------------------
1 | 1 | 2020-04-20 12:00:00 | 2020-04-20 14:00:00
2 | 1 | 2020-04-20 17:00:00 | 2020-04-20 19:30:00
3 | 1 | 2020-04-20 14:30:00 | 2020-04-20 15:40:00
4 | 1 | 2020-04-20 13:00:00 | 2020-04-20 15:00:00
5 | 1 | 2020-04-21 13:00:00 | 2020-04-21 14:00:00
6 | 1 | 2020-04-21 14:00:00 | 2020-04-21 15:00:00
您可以使用 exists
:
select t.*
from t
where exists (select 1
from t t2
where t2.timedate0 < t.timedate2 and
t2.timedate2 > t.timedate0 and
t2.id_user = t.id_user and t2.id <> t.id
);