1

我是 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

感谢您帮助解决这个问题。

4

1 回答 1

0

您的 SQL Server 查询递归地遍历记录以查找属于单个组的所有记录。这样的递归构造在 SQLite 中是不可能的。

但是,可以通过反转检查来找到组:r1如果与之前的记录 ( r2) 的差异超过三分钟(或者如果没有之前的记录),则记录是组中的第一个记录 ( )。一旦我们有了一个组的开始,我们可以通过找到与下一条记录 ( r3) 的差异r4大于三分钟(或没有下一条记录)的第一个后续记录 ( ) 来计算结束:

SELECT r1.start_datetime AS start,
       (SELECT MIN(r3.end_datetime)
        FROM recordings AS r3
        WHERE r3.start_datetime >= r1.end_datetime
          AND IFNULL((SELECT MIN(strftime('%s', r4.start_datetime))
                      FROM recordings AS r4
                      WHERE r4.start_datetime >= r3.end_datetime),
                     99999999999) - strftime('%s', r3.end_datetime) > 3*60
       ) AS end
FROM recordings AS r1
WHERE strftime('%s', r1.start_datetime) -
      IFNULL((SELECT MAX(strftime('%s', r2.end_datetime))
              FROM recordings AS r2
              WHERE r2.end_datetime <= r1.start_datetime),
             0) > 3*60

(这将间隙计算为结束和下一个开始之间的差异,在您的示例中仅为一秒。您可能希望减少 3*60 阈值。)

于 2013-08-29T20:03:27.310 回答