--没想到什么好办法,通过中间表转一下做的 --建表 create table A(updata char(5)) insert A select '18:01' union select '18:03' union select '18:05' union select '18:08' go --创建函数 create function ftest(@updata char(5)) returns char(5) as begin declare @a int declare @b char(5) declare @c char(5) select @a=id from tem where updata=@updata select @b=updata from tem where id=@a-1 select @c=updata from tem where id=@a+1 if datediff(mi,@b,@updata)=2 or datediff(mi,@updata,@c)=2 begin return @updata end else begin return '' end return @updata end go --调用 create table tem(id int identity(1,1),updata char(5)) insert tem select * from A select dbo.ftest(updata) as updata from tem where dbo.ftest(updata)<>'' go--结果 updata ------ 18:01 18:03 18:05 --删除测试环境 drop table A go drop table tem go drop function ftest go
时间不可能一个个插入,而且时间是随机的 什么意思?我写的语句也是一次把所有数据插入到一个新表中,再输出结果,如果要查两分钟之内的数据,只要把这句话 if datediff(mi,@b,@updata)=2 or datediff(mi,@updata,@c)=2 begin 改为if (datediff(mi,@b,@updata) between -2 and 2) or (datediff(mi,@updata,@c)=2 between -2 and 2) begin
列名:updata 行值:18:00 (1)
18:01 (2)
18:03 (3)
18:05 (4)
18:08 (5)我的意思是:第一行和第二行的时间间隔是多少?
还有就是最终要的结果是行和行之间相差2分钟的那条记录,比如说第三行和第四行比较,它就是相差2分钟,我就想知道这些行中相差两分钟的行值.写一条查询语句!!!
因为这个数据库每天都记录着用户信息,所以就想查询一下第三行第四行之间相差两分钟的行值!
行是顺序的,所以只要求第一行和第二行比较,第三行和第四行比较,依此类推!!!
如果按你那么说,就不用显示出来,因为他们相差是五分钟,所以没必要把它查询出来,我要的是相差两分钟的记录........看来大家都是高手啊,我得要向你们多多学习啊...
谢谢了.
--建表
create table A(updata char(5))
insert A select '18:01'
union select '18:03'
union select '18:05'
union select '18:08'
go
--创建函数
create function ftest(@updata char(5))
returns char(5)
as begin
declare @a int
declare @b char(5)
declare @c char(5)
select @a=id from tem where updata=@updata
select @b=updata from tem where id=@a-1
select @c=updata from tem where id=@a+1
if datediff(mi,@b,@updata)=2 or datediff(mi,@updata,@c)=2 begin
return @updata
end
else begin
return ''
end
return @updata
end
go
--调用
create table tem(id int identity(1,1),updata char(5))
insert tem select * from A
select dbo.ftest(updata) as updata from tem where dbo.ftest(updata)<>''
go--结果
updata
------
18:01
18:03
18:05
--删除测试环境
drop table A
go
drop table tem
go
drop function ftest
go
什么意思?我写的语句也是一次把所有数据插入到一个新表中,再输出结果,如果要查两分钟之内的数据,只要把这句话
if datediff(mi,@b,@updata)=2 or datediff(mi,@updata,@c)=2 begin
改为if (datediff(mi,@b,@updata) between -2 and 2) or (datediff(mi,@updata,@c)=2 between -2 and 2) begin