我有一个以毫秒为主键的时间戳的sqlite表,每一行之间应该间隔1秒或1000秒。有时我的数据记录仪坏了,表中没有那个时间的数据。我如何使用SQL语句找到差距?我知道基于游标的解决方案是可能的。
table = PVT
TS
1119636081000
1119636082000
1119636083000
1119636084000
1119636085000
------gap------
1119636090000
1119636091000
这可能有效。假设表名为"tstamps",
select a.ts
from tstamps a
where not exists
(select b.ts
from tstamps b
where b.ts = a.ts+1000)
and exists
(select c.ts
from tstamps c
where c.ts = a.ts+2000)
另一种方式select a.ts
from tstamps a
where not exists
(select b.ts
from tstamps b
where b.ts = a.ts+1000)
and a.ts <
(select max(c.ts)
from tstamps c
)
使用减号运算符。我不确定,这些查询哪一个性能更好。
select ts+1000
from pvt
where ts != (select max(ts) from pvt)
minus
select ts
from pvt
where ts != (select min(ts) from pvt)
类似这样(假设PVT.TS是您的列名):
SELECT * FROM 'table' WHERE PVT.TS ISNULL;
或
SELECT * FROM 'table' WHERE PVT.TS IS NULL;
如果您的收集器实际上正在输入一个空白条目,您可能需要
WHERE PVT.TS = ''
或
where ifnull(some_column, '') = ''
在写这篇文章的时候,SQLite不支持像LAG(TS) OVER (ORDER BY TS ASC)
和LEAD() OVER
这样的窗口函数,它们很容易分别给出前面和后面的TS
值。
所以,你需要自己做:
sqlite> .mode col
sqlite> .width 14 14 14
sqlite> SELECT PVT.TS AS measurement,
prev.TS AS prev,
next.TS AS next
FROM PVT
LEFT JOIN PVT next ON PVT.TS = (next.TS - 1000)
LEFT JOIN PVT prev ON PVT.TS = (prev.TS + 1000);
这将给你类似这样的东西(我使用不同的数据,你会看到):
-- measurement prev next
------------- ------------- -------------
1119636081000 1119636082000 -- gap (no previous at all)
1119636082000 1119636081000 1119636083000
1119636083000 1119636082000 1119636084000
1119636084000 1119636083000 1119636085000
1119636085000 1119636084000 -- gap (no next offset 1000)
1119636088000 1119636089000 -- gap (no previous offset 1000)
1119636089000 1119636088000 -- gap (no next at all)
您始终可以将该查询限制为仅针对WHERE prev.TS IS NULL OR next.TS is NULL
的记录。
创建一个至少包含86400行(每天每秒一行)的统计表:
create table Tally(n integer primary key not null);
insert into Tally(n) values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
insert into Tally(n) select null from tally n1 , tally n2, tally n3, tally n4, tally n5;
将您的PVT表连接到当天的转置记录:
select 1119636081000 + tally.n*1000 as Expected, pvt.ts from tally left join pvt on pvt.ts = 1119636081000 + tally.n*1000 where tally.n <= 86400 limit 15;
给定一个表,我用您的样本数据填充,我得到这个作为输出:
Expected TS
------------- -------------
1119636081000 1119636081000
1119636082000 1119636082000
1119636083000 1119636083000
1119636084000 1119636084000
1119636085000 1119636085000
1119636086000
1119636087000
1119636088000
1119636089000
1119636090000 1119636090000
1119636091000 1119636091000
1119636092000
1119636093000
1119636094000
1119636095000
如果你在PVT.TS为空的地方进行额外的过滤,你应该得到缺失的值:
select 1119636081000 + tally.n*1000 as Expected, pvt.ts from tally left join pvt on pvt.ts = 1119636081000 + tally.n*1000 where tally.n <= 86400 and PVT.ts is null limit 15;
Expected TS
------------- ----------
1119636086000
1119636087000
1119636088000
1119636089000
1119636092000
1119636093000
1119636094000
1119636095000
1119636096000
1119636097000
1119636098000
1119636099000
1119636100000
1119636101000
1119636102000
注意:我使用限制15来保持我在控制台的理智