;with temp (a, intime, outtime) as ( select 1, 10, 12 union all select 1, 11, 14 union all select 1, 15, 16 union all select 2, 13, 15 union all select 2, 12, 14 union all select 3, 08, 11 ) select a.a , count(case when b.a is not null then 1 end) cnt from temp a left join temp b on a.intime <= b.outtime and a.outtime >= b.intime and a.a = b.a and a.intime <> b.intime and a.outtime <> b.outtime group by a.a /* 螳 覯螳 る.. |------------| *----------------> <-------------* 譬襭 企蟆 れ伎る true . |------------| *----------------> <-------------* 譬襭 企蟆 覃 譬襭譟郁唄 true企 false企襦 2螳 譟郁唄 and覃 false螳 . */