我在PostgreSQL中有一个大型数据表,其中包含一个用于开始日期和结束日期的列。对于每一行,我希望对同一表中包含重叠日期范围的其他行数进行计数,并使用这些值创建一个新表。
为了计算重叠的日期范围,我一直计划使用:
where (start_date between [target_start_date] and [target_end_date]) or
(end_date between [target_start_date] and [target_end_date]))但我不知道如何对每一行执行此操作,同时将其与其自己的表进行比较,并计算满足此条件的次数。
任何想法都将不胜感激,谢谢!
发布于 2019-05-24 05:11:44
一种可能性是使用子查询和OVERLAPS运算符。
SELECT t1.*,
(SELECT count(*)
FROM elbat t2
WHERE (t2.target_start_date,
t2.target_end_date) OVERLAPS (t1.target_start_date,
t1.target_end_date))
FROM elbat t1;https://stackoverflow.com/questions/56282910
复制相似问题