将多个记录检索为单个记录

时间:2013-08-29 18:28:27

标签: join sqlite

我是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使用SQLE实现相同的功能,但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

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

1 个答案:

答案 0 :(得分: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阈值。)