首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请教怎么每5分钟取一条数据

2012-12-31 
请问如何每5分钟取一条数据数据类似下面:usridt1其他字段............12012-11-11 11:1112012-11-11 11:12

请问如何每5分钟取一条数据
数据类似下面:
usrid     t1                     其他字段............
12012-11-11 11:11
12012-11-11 11:12
12012-11-11 11:13
12012-11-11 11:14
12012-11-11 11:15
12012-11-11 11:16
12012-11-11 11:17
12012-11-11 11:18
12012-11-11 11:19
12012-11-11 11:20
12012-11-11 11:21
12012-11-11 11:22
12012-11-11 11:23
12012-11-11 11:24
12012-11-11 11:25
12012-11-11 11:26
12012-11-11 11:27
12012-11-11 11:28
12012-11-11 11:29
22012-11-11 11:11
22012-11-11 11:12
22012-11-11 11:13
22012-11-11 11:14
22012-11-11 11:15
22012-11-11 11:16
22012-11-11 11:17
22012-11-11 11:18
22012-11-11 11:19
22012-11-11 11:20
22012-11-11 11:21
22012-11-11 11:22
22012-11-11 11:23
22012-11-11 11:24
22012-11-11 11:25
22012-11-11 11:26
22012-11-11 11:27
22012-11-11 11:28
22012-11-11 11:29

得到结果为

usrid     t1                     其他字段.....
12012-11-11 11:11
12012-11-11 11:16
12012-11-11 11:21
12012-11-11 11:26
22012-11-11 11:11
22012-11-11 11:16
22012-11-11 11:21
22012-11-11 11:26

[解决办法]
create table tb(usrid int,t1 datetime)
insert into tb
select 1,'2012-11-11 11:11' union all
select 1,'2012-11-11 11:12' union all
select 1,'2012-11-11 11:13' union all
select 1,'2012-11-11 11:14' union all
select 1,'2012-11-11 11:15' union all
select 1,'2012-11-11 11:16' union all
select 1,'2012-11-11 11:17' union all
select 1,'2012-11-11 11:18' union all
select 1,'2012-11-11 11:19' union all
select 1,'2012-11-11 11:20' union all
select 1,'2012-11-11 11:21' union all
select 1,'2012-11-11 11:22' union all
select 1,'2012-11-11 11:23' union all
select 1,'2012-11-11 11:24' union all
select 1,'2012-11-11 11:25' union all
select 1,'2012-11-11 11:26' union all
select 1,'2012-11-11 11:27' union all
select 1,'2012-11-11 11:28' union all
select 1,'2012-11-11 11:29' union all
select 2,'2012-11-11 11:11' union all
select 2,'2012-11-11 11:12' union all
select 2,'2012-11-11 11:13' union all
select 2,'2012-11-11 11:14' union all
select 2,'2012-11-11 11:15' union all
select 2,'2012-11-11 11:16' union all
select 2,'2012-11-11 11:17' union all
select 2,'2012-11-11 11:18' union all
select 2,'2012-11-11 11:19' union all
select 2,'2012-11-11 11:20' union all
select 2,'2012-11-11 11:21' union all
select 2,'2012-11-11 11:22' union all
select 2,'2012-11-11 11:23' union all
select 2,'2012-11-11 11:24' union all


select 2,'2012-11-11 11:25' union all
select 2,'2012-11-11 11:26' union all
select 2,'2012-11-11 11:27' union all
select 2,'2012-11-11 11:28' union all
select 2,'2012-11-11 11:29'
go
;with cte as(
select * from tb a where not exists(select 1 from tb where usrid=a.usrid and t1<a.t1)
union all
select a.* from tb a inner join cte b on a.usrid=b.usrid and datediff(mi,b.t1,a.t1)=5
)
select * from cte order by usrid,t1
/*
usrid       t1
----------- -----------------------
1           2012-11-11 11:11:00.000
1           2012-11-11 11:16:00.000
1           2012-11-11 11:21:00.000
1           2012-11-11 11:26:00.000
2           2012-11-11 11:11:00.000
2           2012-11-11 11:16:00.000
2           2012-11-11 11:21:00.000
2           2012-11-11 11:26:00.000

(8 行受影响)
*/
go
drop table tb


[解决办法]
declare @tb table (usrid int,t1 datetime)
insert into @tb
select 1,'2012-11-11 11:11' union all 
select 1,'2012-11-11 11:12' union all 
select 1,'2012-11-11 11:13' union all 
select 1,'2012-11-11 11:14' union all
select 1,'2012-11-11 11:15' union all 
select 1,'2012-11-11 11:16' union all 
select 1,'2012-11-11 11:17' union all 
select 1,'2012-11-11 11:18' union all
select 1,'2012-11-11 11:19' union all 
select 1,'2012-11-11 11:20' union all 
select 1,'2012-11-11 11:21' union all 
select 1,'2012-11-11 11:22' union all
select 1,'2012-11-11 11:23' union all 
select 1,'2012-11-11 11:24' union all 
select 1,'2012-11-11 11:25' union all 
select 1,'2012-11-11 11:26' union all
select 1,'2012-11-11 11:27' union all 
select 1,'2012-11-11 11:28' union all 
select 1,'2012-11-11 11:29' union all 
select 2,'2012-11-11 11:11' union all
select 2,'2012-11-11 11:12' union all 
select 2,'2012-11-11 11:13' union all 
select 2,'2012-11-11 11:14' union all 
select 2,'2012-11-11 11:15' union all
select 2,'2012-11-11 11:16' union all 
select 2,'2012-11-11 11:17' union all 
select 2,'2012-11-11 11:18' union all 
select 2,'2012-11-11 11:19' union all
select 2,'2012-11-11 11:20' union all 
select 2,'2012-11-11 11:21' union all 
select 2,'2012-11-11 11:22' union all 


select 2,'2012-11-11 11:23' union all
select 2,'2012-11-11 11:24' union all 
select 2,'2012-11-11 11:25' union all 
select 2,'2012-11-11 11:26' union all 
select 2,'2012-11-11 11:27' union all
select 2,'2012-11-11 11:28' union all 
select 2,'2012-11-11 11:29'
if OBJECT_ID('tb') is not null   
drop table tb 
;with tb as
(
select * from @tb a where a.usrid not in(select usrid from @tb where usrid=a.usrid and t1<a.t1)
union all
select a.* from @tb a,tb b where a.usrid=b.usrid and datediff(mi,b.t1,a.t1)=5
)
select * from tb order by usrid,t1

请教怎么每5分钟取一条数据
[解决办法]

引用:
引用:
declare @tb table (usrid int,t1 datetime)
insert into @tb
select 1,'2012-11-11 11:11' union all 
select 1,'2012-11-11 11:12' union all 
select 1,'2012-11-11 11:13' unio……


select * from tb a where not exists(select 1 from tb where usrid=a.usrid and t1<a.t1)
这个是取id相同的一组数据中最大的t1 
select a.* from tb a inner join cte b on a.usrid=b.usrid and 
datediff(mi,b.t1,a.t1)=5
这个查询时间跨度为5分钟的数据,注意是要内联上面一个查询的

然后使用union all 连合起来
[解决办法]
我感觉 如果你数据库里存的是每个一分钟一条数据 有规律的话

直接截取t1这个时间字符串不就好了 

具体:截取最后两位  转化为int 减1 然后除以5 取余数 如果余数是0 说明就是符合条件的数据

能出来,可能很不主流,你要是时间不够的话 可以考虑这个方法

热点排行