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......................
解决方案 »
- 如何将mysql数据导入到SQL SERVER 2005 及字符编号问题!
- 请问各位大侠,这个sql怎么写?谢谢啊!!!
- 建表的问题
- 弱弱地问一个:可否有快捷方式取得刚Insert进去那条数据的ID(ID是表的一个INT自增量主键)????
- SQL通信问题
- SQL ACCESS
- 一个数据库任务的问题
- 求助, 怎样在SQL server 7.0 上实现级联删除???
- 能通过一条sql语句实现这样的功能码。。。。。。
- 急!新手请教:有个价格信息表由3个字段组成名称(name),金额(price),日期(IssueTime),想得到一个由名称,year1总金额,year2...总金额组成的各年度总金额统计表,不知SQL语句该怎么写?
- 数据如何重新分行列,可能我太菜,感觉超有难度
- 求查询函数
(
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 行受影响)
*/
一个按五分钟分段统计的例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