就是说如果一天24小时都有数据,比如每个小时的时间都显示出来,那就表示没有退服,如果之间有时间间隔,就表示有退服了
a    2006-1-19 01:00:00--退服开始时间
 a    2006-1-19 06:00:00--退服结束时间这里不连续,就表示从1点开始退服,然后6点结束了退服时间,如果都是联系的时间,比如
a    2006-1-19 01:00:00--退服开始时间
a    2006-1-19 02:00:00--退服结束时间
a    2006-1-19 03:00:00--退服开始时间
a    2006-1-19 04:00:00--退服结束时间
...全部连续,间隔为一个小时
a    2006-1-19 23:00:00--退服开始时间
就表示19号这天没有退服

解决方案 »

  1.   

    cell          time
    a    2006-1-19 00:00:00
    a    2006-1-19 01:00:00--退服开始时间
    a    2006-1-19 06:00:00--退服结束时间
    a    2006-1-19 07:00:00
    a    2006-1-19 08:00:00--退服开始时间
    a    2006-1-19 22:00:00--退服结束时间
    a    2006-1-19 23:00:00我怎么样统计一天的退服次数呢?
    ------------------------------
    你需要加退服标记列吧 要不怎么统计呢 如果你所指的退服次数需要算上当天下次登服的时间 直接求和有点问题
      

  2.   

    其实这是个很简单的问题啊 把表设计好select...count..where不就好了?
      

  3.   

    业务是这样的,数据库里面有两个字段
    ,一个是cell,一个是time,数据库里面有现成的数据,每天都有24个小时的数据记录,如果有哪个小时没有,就不会存入数据库,比如19号那天,数据库就有这些数据,那么19号的退服时间如下
    那么如果小时之间不联系,就表示有退服,退服就表示有机器在那个时间段内没有工作了,比如19号1点后没有跟2点,那么就表示有机器推出服务了,那么就是退服开始了,然后再6点又有数据了,表示机器又开始工作了,那么1点到6点就是退服的时间,然后根据上面的公司来开始计算,不知道这样说明白否?
    cell  time
     a    2006-1-19 00:00:00
     a    2006-1-19 01:00:00--退服开始时间
     a    2006-1-19 06:00:00--退服结束时间
     a    2006-1-19 07:00:00
     a    2006-1-19 08:00:00--退服开始时间
     a    2006-1-19 22:00:00--退服结束时间
     a    2006-1-19 23:00:00
      

  4.   


    create table #1 (cell varchar(10),time datetime)insert into #1(cell,time) select 
     'a' ,   '2006-1-19 00:00:00'
    union all select
     'a' ,   '2006-1-19 01:00:00'--退服开始时间
    union all select
     'a' ,   '2006-1-19 06:00:00'--退服结束时间
    union all select
     'a'  ,  '2006-1-19 07:00:00'
    union all select
     'a'  ,  '2006-1-19 08:00:00'--退服开始时间
    union all select
     'a'  ,  '2006-1-19 22:00:00'--退服结束时间
    union all select
     'a'  , '2006-1-19 23:00:00'
    Declare @times int
    Declare @TempDate datetime
    Declare @TempDate1 datetime
    --这里比如要查找2006-1-19号
    set @times=0update #1 set @tempdate=time,@times=case when @tempdate<>dateadd(hh,1,@tempdate1) then @times+1 else @times end,
    @tempdate1=@tempdate
    where time between '2006-1-19' and '2006-1-19 23:59'
    select @timesselect * from #1
      

  5.   

    或者这样,如果只是找一天的话,create table #1 (cell varchar(10),time datetime)insert into #1(cell,time) select 
     'a' ,   '2006-1-19 00:00:00'
    union all select
     'a' ,   '2006-1-19 01:00:00'--退服开始时间
    union all select
     'a' ,   '2006-1-19 06:00:00'--退服结束时间
    union all select
     'a'  ,  '2006-1-19 07:00:00'
    union all select
     'a'  ,  '2006-1-19 08:00:00'--退服开始时间
    union all select
     'a'  ,  '2006-1-19 22:00:00'--退服结束时间
    union all select
     'a'  , '2006-1-19 23:00:00'
    Declare @times int
    Declare @TempDate datetime
    --这里比如要查找2006-1-19号
    set @times=0select  @tempdate=[time],@times=case when @tempdate<>dateadd(hh,1,@tempdate1) then @times+1 else @times end,
    @tempdate1=@tempdate from #1
    where time between '2006-1-19' and '2006-1-19 23:59'
    select @timesdrop table #1
      

  6.   

    如果统计为按日期,cell汇总统计,可以用这个
    create table #1 (cell varchar(10),time datetime)insert into #1(cell,time) select 
     'a' ,   '2006-1-19 00:00:00'
    union all select
     'a' ,   '2006-1-19 01:00:00'--退服开始时间
    union all select
     'a' ,   '2006-1-19 06:00:00'--退服结束时间
    union all select
     'a'  ,  '2006-1-19 07:00:00'
    union all select
     'a'  ,  '2006-1-19 08:00:00'--退服开始时间
    union all select
     'a'  ,  '2006-1-19 22:00:00'--退服结束时间
    union all select
     'a'  , '2006-1-19 23:00:00'
    --注意这里2006-1-19 00:00:00会被计算一次,所以多一次
    select time,times=count(cell) from
    (select cell,time=convert(varchar(10),time,120) from #1 a
    where not exists(select 1 from #1 where cell=a.cell and [time]=dateadd(hh,-1,a.time)))a
    group by a.time,cell
      

  7.   

    ReViSion(和尚) ( ) 
    按你的方法,请问当"a    2006-1-19 23:00:00"也处于退服状态呢?
      

  8.   

    create table #1 (cell varchar(10),time datetime)insert into #1(cell,time) select 
     'a' ,   '2006-1-19 00:00:00'
    union all select
     'a' ,   '2006-1-19 01:00:00'--退服开始时间
    union all select
     'a' ,   '2006-1-19 06:00:00'--退服结束时间
    union all select
     'a'  ,  '2006-1-19 07:00:00'
    union all select
     'a'  ,  '2006-1-19 08:00:00'--退服开始时间
    union all select
     'a'  ,  '2006-1-19 22:00:00'--退服结束时间
    union all select
    'a'  , '2006-1-19 23:00:00'
    union all select
    'a'  , '2006-1-20 00:00:00'select IDENTITY(int) AS ID,A.cell AS CELL,A.[time] AS TIME  into #2 from #1 a 
    select * from 
    (SELECT A.CELL,DATENAME (HH,B.TIME-A.TIME)  AS HH FROM #2 AS A,#2 AS B WHERE A.ID+1=B.ID 
    and a.time>='2006-1-19' and b.time<='2006-1-20')C
    WHERE C.HH>1
    --然后在分组取4-8,8-12,12-14 等分段数据
    drop table #1
    drop table #2