当前位置: 代码迷 >> Sql Server >> 求任意几条连续时间的记录,该怎么处理
  详细解决方案

求任意几条连续时间的记录,该怎么处理

热度:244   发布时间:2016-04-27 19:26:50.0
求任意几条连续时间的记录
表格如下:
id time num  
01 9:01  
01 9:02 
01 9:04 
01 9:06 
01 9:10 
01 9:12 
如取4条

希望得到结果
01 9:01  
01 9:02 
01 9:04 
01 9:06 


01 9:02 
01 9:04 
01 9:06 
01 9:10 

01 9:04 
01 9:06 
01 9:10 
01 9:12 


------解决方案--------------------
借用楼上朋友的测试数据

SQL code
declare @T table(id varchar(2),time varchar(10))insert @Tselect '01','9:01' union allselect '01','9:02' union allselect '01','9:04' union allselect '01','9:06' union allselect '01','9:10' union allselect '01','9:12'select *,identity(int) nid into # from @t order by timeselect top 4 * from # where nid>=(select cast(rand(checksum(newid()))*(max(nid)-3) as int) from #)drop table #
------解决方案--------------------
SQL code
--哦,随机4条连续--原始数据:@Tdeclare @T table(id varchar(2),time varchar(10))insert @Tselect '01','9:01' union allselect '01','9:02' union allselect '01','9:04' union allselect '01','9:06' union allselect '01','9:10' union allselect '01','9:12'declare @num int,@row_num intset @num=4select row_num=identity(int),* into #T from @T order by timeset @row_num=cast((ident_current('tempdb.dbo.#T')[email protected]+1)*rand() as int)--哦,随机N条连续的:exec ('select top [email protected]+' id,time from #T where row_num>[email protected]_num)/*id   time       ---- ---------- 01   9:0201   9:0401   9:0601   9:10*/--删除临时表drop table #T
------解决方案--------------------
是否要得到任意的几条连接记录.
是否指定一时间,要得到>=指定时间后的连接几条记录.
查询的关键是对时间排序.
首先要知道时间的格式
对>9的时间,是否表示为hh:mm的格式的字符串,如是,
对时间排序可用
order by right('0'+time,5)
对时间排序
请LZ再详细说明要查询的结果.

  相关解决方案