| yueyangflash | | 2008-3-25 06:10 |
|
求助一个关于取时间段间隔的语句
2008-03-23 10:10:15.000
2008-03-23 10:12:17.000
2008-03-23 10:12:33.000
2008-03-23 10:13:37.000
2008-03-23 10:42:39.000
2008-03-23 10:42:41.000
2008-03-23 10:43:42.000
2008-03-23 10:44:38.000
2008-03-23 11:00:05.000
2008-03-23 11:01:19.000
2008-03-23 11:01:36.000
2008-03-23 11:02:11.000
2008-03-23 11:23:22.000
2008-03-23 11:45:06.000
要求:
以上时间段是一个例子,我的要求是,整个数据库按时间字段排序,然后判断出时间间隔大于20分钟的时间段,
例如:
2008-03-23 10:13:37.000
2008-03-23 10:42:39.000
2008-03-23 11:02:11.000
2008-03-23 11:23:22.000
2008-03-23 11:45:06.000
DATEDIFF(minute,LastVisitTime,getdate())>20这个函数虽然符合一点要求,但是不行,他取得是当前时间,
我要判断的是上一条记录和下一条记录的时间间隔大于20分钟的!
请帮忙看下!谢谢! |
|
[color=red][b]此回复于2008-04-07 11:19被 [url=http://bbs.51cto.com/profile-uid-153663.html]yueyangflash[/url] 评为最佳答案[/b][/color]
要使用自连接。
在sqlserver2005中使用排序函数可以轻松实现:
select a.LastVisitTime,b.LastVisitTime from
(select ROW_NUMBER() OVER(ORDER BY LastVisitTime ASC) AS 'RowNumber',LastVisitTime
FROM tablename ) a,
(select ROW_NUMBER() OVER(ORDER BY LastVisitTime ASC) AS 'RowNumber',LastVisitTime
FROM tablename ) b
where a.RowNumber=b.RowNumber-1 and DATEDIFF(minute,a.LastVisitTime,b.LastVisitTime)>20
--注:在sql server 2000中同样的思路实现可能要用到临时表+identity自增一列来进行 |
| yueyangflash | | 2008-3-27 04:25 |
|
create table tb(时间 datetime)
insert into tb values('2008-03-23 10:10:15.000')
insert into tb values('2008-03-23 10:12:17.000')
insert into tb values('2008-03-23 10:12:33.000')
insert into tb values('2008-03-23 10:13:37.000')
insert into tb values('2008-03-23 10:42:39.000')
insert into tb values('2008-03-23 10:42:41.000')
insert into tb values('2008-03-23 10:43:42.000')
insert into tb values('2008-03-23 10:44:38.000')
insert into tb values('2008-03-23 11:00:05.000')
insert into tb values('2008-03-23 11:01:19.000')
insert into tb values('2008-03-23 11:01:36.000')
insert into tb values('2008-03-23 11:02:11.000')
insert into tb values('2008-03-23 11:23:22.000')
insert into tb values('2008-03-23 11:45:06.000')
go
select m.时间 开始时间 , n.时间 结束时间 from
(select 时间 , px = (select count(*) from tb where 时间 < t.时间) + 1 from tb t) m,
(select 时间 , px = (select count(*) from tb where 时间 < t.时间) + 1 from tb t) n
where m.px = n.px - 1 and datediff(mi , m.时间 , n.时间) > 20
这个语句虽然可以达到我得要求!但是还有一点问题!
create table tb(时间 datetime,编号)
insert into tb values('2008-03-23 10:10:15.000',1)
insert into tb values('2008-03-23 10:12:17.000',1)
insert into tb values('2008-03-23 10:12:33.000',1)
insert into tb values('2008-03-23 10:13:37.000',1)
insert into tb values('2008-03-23 10:42:39.000',1)
insert into tb values('2008-03-23 10:42:41.000',1)
insert into tb values('2008-03-23 10:43:42.000',1)
insert into tb values('2008-03-23 10:44:38.000',1)
insert into tb values('2008-03-23 11:00:05.000',1)
insert into tb values('2008-03-23 11:01:19.000',2)
insert into tb values('2008-03-23 11:01:36.000',2)
insert into tb values('2008-03-23 11:02:11.000',2)
insert into tb values('2008-03-23 11:23:22.000',2)
insert into tb values('2008-03-23 11:45:06.000',2)
我想先查出编号为“1”的所有数据,然后按照这个再行筛选!
谢谢! |
| yueyangflash | | 2008-4-7 03:18 |
|
| 没有适合我的!只有CSDN上面有个合适的! |
关键词: 灾 求助 图 c
相关文章: 求助!!!!!! 冒死推荐:有史以来最新刺激的舞曲,雪村作 张雨生声音重现乐坛 张羽《落叶》天籁之极 电脑主板BIOS设置 黑客技术文档(2) 中国500强企业——东岭集团携手瑞友共建企业信息化
Powered by 51CTO.COM
|