--测试数据
create table #t(LogTime varchar(20),LogValue int)insert #t select '2005-10-11 01:01:01',2
insert #t select '2005-10-11 01:01:01',4
insert #t select '2005-10-11 02:01:01',1
insert #t select '2005-10-11 02:01:01',3
insert #t select '2005-10-11 03:01:01',2
insert #t select '2005-10-11 03:01:01',7--查询
select substring(LogTime,12,2)+':00~'+right(101+substring(LogTime,12,2),2)+':00' 时间,sum(LogValue) 值 from #t group by substring(LogTime,12,2)+':00~'+right(101+substring(LogTime,12,2),2)+':00'--结果
时间              值           
--------------- ----------- 
01:00~02:00     6
02:00~03:00     4
03:00~04:00     9(所影响的行数为 3 行)

解决方案 »

  1.   

    select substring(logtime,12,2),sum(logvalue) from tablename group by logtime
      

  2.   

    问题再度升级:
    LogTime varchar 20,
    LogValue int 4,
    flag smallint,
    devicename varchar 256
    数据如下:LogTime                LogValue flag     deviceName
    2005-10-11 01:01:01     2        1         device1
    2005-10-11 01:01:01     4        1         device1
    2005-10-11 01:01:01     3        0         device1
    2005-10-11 01:01:01     5        0         device1
    2005-10-11 01:01:01     1        0         device2
    2005-10-11 01:01:01     4        0         device2
    2005-10-11 02:01:01     1        1         device1
    2005-10-11 02:01:01     3        1         device1
    2005-10-11 02:01:01     1        0         device1
    2005-10-11 02:01:01     3        0         device1
    2005-10-11 03:01:01     2        1         device1
    2005-10-11 03:01:01     7        1         device1
    2005-10-11 03:01:01     2        0         device1
    2005-10-11 03:01:01     7        0         device1要求:计算结果如下例如flag:0表示上行,1表示下行,进行分时段,对deviceName=device1的上行和下行数据分别求和!
    结果如下:
    logtime          上行(flag=0)   下行(flag=1)
    00:00~01:00            8            6
    01:00~02:00            4            4
    02:00~03:00            9            9
    请各位高手帮忙