在SQL中合并间隔 - 奇怪的行为

时间:2017-05-22 19:07:25

标签: mysql sql algorithm intervals

我在使用MySQL 5.5 / 5.6时遇到了一个奇怪的问题。 我在SQL中实现了一个经典的合并区间算法,但它并没有像预期的那样在某些行上运行。我使用Merging intervals in one pass in SQLMost efficient way to sum overlapping datetimes in MySQL作为起始参考(C ++ @ http://www.geeksforgeeks.org/merging-intervals/

任何人都能发现任何问题吗?

SELECT MIN(inizio) as inizio, MAX(fine) as fine 
FROM (
    SELECT 
    @interval_id := IF(attendances.inizio > @interval_end, @interval_id + 1, @interval_id) AS interval_id, 
    @interval_end := IF(attendances.inizio < @interval_end, GREATEST(@interval_end, attendances.fine), attendances.fine) AS interval_end, 
    attendances.inizio, 
    attendances.fine 
    FROM attendances 
    INNER JOIN attendance_sheets ON ( attendance_sheet_id = attendance_sheets.id) 
    ORDER BY attendances.inizio,attendances.fine 
) intervals GROUP BY interval_id;

从这些行

enter image description here

我跳过了@interval_id而错误的@interval_end

enter image description here

导致2017-01-26错误合并

enter image description here

提前谢谢 尼古拉

0 个答案:

没有答案