我是 SQLite 的新手。我正在使用 SQLite Manager Firefox 插件。我创建了一个数据库和一个表。此表存储网络摄像头记录。每条记录最长为 3 分钟。我正在尝试每次连续记录检索一条记录。如果间隔超过 3 分钟,则将其视为单独录制。下面是脚本。
CREATE TABLE recordings (
[key] INTEGER PRIMARY KEY ASC AUTOINCREMENT,
filename VARCHAR(50),
start_datetime DATETIME,
end_datetime DATETIME,
deleted BOOLEAN
);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f1', '2013-08-26 00:00:00', '2013-08-26 00:03:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f2', '2013-08-26 00:03:01', '2013-08-26 00:06:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f3', '2013-08-26 00:06:01', '2013-08-26 00:09:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f4', '2013-08-26 00:14:00', '2013-08-26 00:17:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f5', '2013-08-26 00:17:01', '2013-08-26 00:20:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f6', '2013-08-26 00:20:01', '2013-08-26 00:23:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f7', '2013-08-26 00:30:00', '2013-08-26 00:33:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f8', '2013-08-26 00:33:01', '2013-08-26 00:36:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f9', '2013-08-26 00:36:01', '2013-08-26 00:39:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f10', '2013-08-26 00:44:00', '2013-08-26 00:47:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f11', '2013-08-26 00:47:01', '2013-08-26 00:50:00', 0);
INSERT INTO [recordings] ([filename], [start_datetime], [end_datetime], [deleted]) VALUES ('f12', '2013-08-26 00:50:01', '2013-08-26 00:53:00', 0);
结果应该如下所示,
recording1 2013-08-26 00:00:00 2013-08-26 00:09:00
recording2 2013-08-26 00:14:00 2013-08-26 00:23:00
recording3 2013-08-26 00:30:00 2013-08-26 00:39:00
recording4 2013-08-26 00:44:00 2013-08-26 00:53:00
我尝试使用 CTE 使用 SQL Server 来实现相同的目标,但 SQLite 不支持它。下面是CTE方式,
with cte
as
(
select [KEY],start_datetime,end_datetime,1 as recodringno from [dbo].[recordings] where [KEY] =1
union all
select a.[KEY],a.start_datetime,a.end_datetime
,case when DATEDIFF(MINUTE,b.end_datetime,a.end_datetime)>3 then b.recodringno+1 else b.recodringno end
as recodringno from
[dbo].[recordings] a
inner join cte b on a.[KEY]=b.[KEY]+1
)
select 'recodring'+cast(recodringno as varchar(10)) as recodringno
,MIN(start_datetime)start_datetime,MAX(end_datetime) end_datetime from cte group by recodringno
感谢您帮助解决这个问题。