sorry cant see how to edit my post
last table should be populated thus
insert @output
select 2,'1 sep 2008',500,530,0
union all select 1,'1 sep 2008',530,575,1 --overlap
union all select 3,'1 sep 2008',545,560,1 --overlap
union all select 4,'2 sep 2008',840,930,0
union all select...