Detect Anomaly Intervals with SQL

后端 未结 4 1252
猫巷女王i
猫巷女王i 2021-02-06 11:26

My problem is simple: I have a table with a series of statuses and timestamps (for the sake of curiosity, these statuses indicate alarm levels) and I would like to query this ta

4条回答
  •  悲哀的现实
    2021-02-06 12:15

    I do something similar by using id that is an identity to the table.

        create table test(id int primary key identity(1,1),timstamp datetime,val int)
    
        insert into test(timstamp,val) Values('1/1/2013 00:00:00',1)
        insert into test(timstamp,val) Values('1/1/2013 00:00:05',2)
        insert into test(timstamp,val) Values('1/1/2013 00:00:25',1)
        insert into test(timstamp,val) Values('1/1/2013 00:00:30',2)
        insert into test(timstamp,val) Values('1/1/2013 00:00:35',1)
    
        select t1.timstamp,t1.val,DATEDIFF(s,t1.timstamp,t2.timstamp) 
        from test t1 left join test t2 on t1.id=t2.id-1
    
        drop table test
    

    I would also make the timestamps be seconds since 1980 or 2000 or whatever. But then you might not want to do the reverse conversion all the time and so it depends on how often you use the actual time stamp.

提交回复
热议问题