Create table #Traffic
(
TransTime datetime,
Speed float
)Declare @row as int
set @row=1
While(@row<100000)
begin
Insert into #Traffic
Select DateAdd(SS,2,'2010-01-01'),rand()*50
set @row=@row+1
end
请大家帮忙给个查询语句来查询每5分钟,10分钟或15分钟的流量(记录数量)和平均车速。结果格式如下:
__________________________________________________________________
Time                          Count     AverageSpeed
__________________________________________________________________
2010-01-01 00:00:00            1000         20.3......................

解决方案 »

  1.   

    Create table #Traffic
    (
        TransTime datetime,
        Speed float
    )
    insert into #traffic select '2010-10-10 15:32:24',78
     union all select '2010-10-10 15:34:05',82
     union all select '2010-10-10 15:36:12',92
     union all select '2010-10-10 15:38:12',86
    go
    select 起始时间,结束时间,count(*) as [5分钟流量],avg(speed)as 平均车速 from(
    select convert(varchar(14),transtime,120)+convert(varchar,(datepart(mi,TransTime)/5)*5)+':00' as 起始时间,
      convert(varchar(14),transtime,120)+convert(varchar,(datepart(mi,TransTime)/5+1)*5)+':00' as 结束时间,
      speed
    --如要求10分钟或15分钟的,把上面/5 *5改成/10 *10 或/15 *15
    from #traffic
    )t group by 起始时间,结束时间
    go
    drop table #traffic
    /*
    起始时间                                            结束时间                                            5分钟流量       平均车速
    ----------------------------------------------- ----------------------------------------------- ----------- ----------------------
    2010-10-10 15:30:00                             2010-10-10 15:35:00                             2           80
    2010-10-10 15:35:00                             2010-10-10 15:40:00                             2           89(2 行受影响)
    */
      

  2.   


    一个按五分钟分段统计的例create table tb(时间 datetime , 金额 int)
    insert into tb values('2007-1-1 10:00:23' ,          8 )
    insert into tb values('2007-1-1 10:01:24' ,          4 )
    insert into tb values('2007-1-1 10:05:00' ,          2 )  
    insert into tb values('2007-1-1 10:06:12' ,          3 )
    insert into tb values('2007-1-1 10:08:00' ,          1 )
    insert into tb values('2007-1-1 10:12:11' ,          5 )
    go--时间段>=10:00:00 and 时间段<10:05:00
    select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段,
           count(*) as 行数,
           sum(金额) as 总金额
    from tb
    group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120))
    /*
    时间段                                                    行数          总金额         
    ------------------------------------------------------ ----------- ----------- 
    2007-01-01 10:00:00.000                                3           14
    2007-01-01 10:05:00.000                                2           4
    2007-01-01 10:10:00.000                                1           5
    (所影响的行数为 3 行)
    */--时间段>10:00:00 and 时间段<=10:05:00
    select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段,
           count(*) as 行数,
           sum(金额) as 总金额
    from tb
    group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120))
    /*
    时间段                                                    行数          总金额         
    ------------------------------------------------------ ----------- ----------- 
    2007-01-01 10:00:00.000                                2           12
    2007-01-01 10:05:00.000                                3           6
    2007-01-01 10:10:00.000                                1           5(所影响的行数为 3 行)
    */drop table tb你最好:
    最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
    如果有多表,表之间如何关联?
    发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  3.   

    qianjin036a 的方法用起来比较简单,不知有没有更好的解决方案,继续等待朋友们讨论,明天揭帖...