select datepart(hour,getdate()) as hour,count(*) as count from log group by datepart(hour,getdate())

解决方案 »

  1.   

    //问题2:凌晨时,可能没有用户访问,故将0时的count显示成0這樣設置是可以的,但我覺得這樣做不對。因為不是絕對沒有人訪問
      

  2.   

    楼主的意思是0点是没人访问,就可能会没有0点的记录,这时要显示一条记录为Hour   count
    0       0
      

  3.   

    yelook(雨枫)这样做可能小时会不连续.
      

  4.   

    不是,说错了,yelook(雨枫)这样可能会没有0点的记录
      

  5.   

    drop table #t1
    create table #t1 
    (
    h int unique
    )
    declare @hh int
    set @hh = 0
    while(@hh < 24)
    begin
    insert into #t1(h) values (@hh)
    set @hh = @hh + 1
    endselect #t1.h, count(all ip)
    from  #t1 left join log 
    on #t1.h=datepart(hour,log.[hour])
    group by #t1.h
    order by #t1.h
      

  6.   

    我也不大清楚,是不是这样呢??
    select datepart(hour,getdate()) as hour,count(*) as count ,isnull(count,0)
    from log 
    group by hour
      

  7.   


    drop table #t1
    create table #t1 
    (
    h int unique
    )
    declare @hh int
    set @hh = 0
    while(@hh < 24)
    begin
    insert into #t1(h) values (@hh)
    set @hh = @hh + 1
    end
    delete  from log where (datepart(Hour,hour)) = 0select #t1.h, count(all ip)
    from  #t1 left join log
    on #t1.h=datepart(hour,log.[hour])
    group by #t1.h
    order by #t1.h