TSQL get overlapping periods from datetime ranges
问题 I have a table with date range an i need the sum of overlapping periods (in hours) between its rows. This is a schema example: create table period ( id int, starttime datetime, endtime datetime, type varchar(64) ); insert into period values (1,'2013-04-07 8:00','2013-04-07 13:00','Work'); insert into period values (2,'2013-04-07 14:00','2013-04-07 17:00','Work'); insert into period values (3,'2013-04-08 8:00','2013-04-08 13:00','Work'); insert into period values (4,'2013-04-08 14:00','2013-04