select 
sum(case when plantime>=start01 and completetime<=End01 then 1 else 0 end) as 时间段1, 
sum(case when plantime>=start02 and completetime<=End02 then 1 else 0 end) as 时间段2, 
sum(case when plantime>=start03 and completetime<=End03 then 1 else 0 end) as 时间段3
from  ItemNO表。

解决方案 »

  1.   

    这个好象用不着case when这么麻烦吧...
      

  2.   

    “试用最简单的方法,统计表中plantime和completetime在同一
    时间段的次数。”好像很歧义啊,举一个例子出来。另外,时段定义不放在表中吗?如果是不分时段,统计所有时段select count(itemno)
    from 表
    where ((plantime between start01 and end01) and (completetime between start01 and end01)) 
    or ((plantime between start02 and end02) and (completetime between start02 and end02)) 
    or ((plantime between start03 and end03) and (completetime between start03 and end03)) 
      

  3.   

    select count(itemno)
    from 表1 a
    where exists (
    select * from 表2 b where ((a.plantime between b.starttime and b.endtime) and (a.completetime between b.starttime and b.endtime)) That's All!