declare @t table(usr int, task int, dbeg datetime, dend datetime)
insert into @t
select 1, 1, '20130101 00:00','20130104 14:00'
union
select 1, 1, '20130105 08:00','20130105 10:00'
union
select 1, 2, '20130102 09:00','20130102 11:00'
union
select 1, 3, '20130102 10:00','20130104 16:00'
declare @tt table(usr int, task int, dt datetime, inc int, n int identity primary key clustered)
insert @tt
select usr, task, dbeg, 1 from @t
union all
select usr, task, dend, -1 from @t
order by usr, dbeg, task
select x.usr, x.cnt, x.dt as beg, y.dt as [end]
from
(select t.usr, t.task, t.dt, t.inc, t.n, sum(tt.inc) as cnt
from @tt t left outer join @tt tt on t.usr=tt.usr and t.n>=tt.n
group by t.usr, t.task, t.dt, t.inc, t.n
) x
inner join
@tt y
on x.usr=y.usr and x.n+1=y.n