日期:2014-05-17  浏览次数:20413 次

SQL SERVER 根据时间查询问题,超难,求解!
现在碰到一个问题,做了一个服务器接收设备端的数据,可以接收多个设备,设备每分钟往服务器发送数据,服务器接收数据后,存入数据库,如果没有收到数据,就发送命令让设备重传;

现在问题是,
1)我怎么判断数据缺测,即数据库少了某一分钟的数据,数据库有没有这样的功能,通过SQL语句查询某一段时间内的数据,可以很方便知道哪些分钟的数据丢失,我只要知道丢失的时间,就可以从设备上重新获取数据;

2)如果不能判断,怎么通过插入的时候,判断数据是否有丢失的现象;因为服务器有能断线,重启,等等,很多异常情况?
有没有好的办法解决;

要是能解决第1条,那是最好最安全的方法了。



------解决方案--------------------
设备在向服务器传送数据的时候,增加日期时间字段,格式为 '2012-08-25 14:00'。
则可用下方方法来检测
SQL code

declare @test table(data int,dt varchar(20))
declare @tmp table(sn varchar(2))
declare @i int
set @i=1
while @i<60
begin
    insert into @tmp values(cast(right(100+@i,2) as varchar(2)))
    set @i=@i+1
end
set @i=1
while @i<60
begin
    if @i<>5 and @i<>16
        insert into @test values(@i*@i,'2012-08-25 14:'+cast(right(100+@i,2) as varchar(2)))
    set @i=@i+1
end
select * from
(
    select a.sn sn1,b.sn sn2,b.data,b.dt from @tmp a
    left join (select right(dt,2) sn,* from @test) b
    on a.sn=b.sn
)t
where sn2 is null
--where datediff(hh,b.dt+'.000',getdate())=1

------解决方案--------------------
探讨
谢谢你的回复,我好久没有玩数据库了,最近一个项目需要用到数据库,我在学学存储过程看看,还有请教下,你的查询速度怎样,比如我有几百万条记录,能忙的过来吗?


引用:
设备在向服务器传送数据的时候,增加日期时间字段,格式为 '2012-08-25 14:00'。
则可用下方方法来检测

SQL code


declare @test table(data int……