我有一个带有一些字段的表BOOKDOCUMENT(Id,documentDate);
documentDate的格式为:“YYYY-MM-DD HH:MM:SS.SSS”。
我想获得表格的每个documentDate之间的平均值。
所以我的想法是压缩每个documentDate。
例子:行的最后一行 - 1 =它给出我的第一个值 最后一行 - 1与最后一行-2 =它给出我的第二个值
SELECT julianday(DocumentDate) -
(
SELECT julianday(DocumentDate)
FROM BOOKDOCUMENT
WHERE Id = 25
) AS daysBetweenDoc
FROM BOOKDOCUMENT
WHERE Id = 26
UNION ALL
SELECT julianday(DocumentDate) -
(
SELECT julianday(DocumentDate)
FROM BOOKDOCUMENT
WHERE Id = 24
) AS daysBetweenDoc
FROM BOOKDOCUMENT
WHERE Id = 25;
它正在寻找但我怎样才能为其他行增值? 我想我必须使用CTE
答案 0 :(得分:0)
您可以使用correlated subquery
查找下一个较小的日期SELECT Id,
DocumentDate,
julianday(DocumentDate) -
(SELECT DocumentDate
FROM BookDocument AS T2
WHERE T2.DocumentDate < BookDocument.DocumentDate
ORDER BY DocumentDate DESC
LIMIT 1
) AS daysBetweenDoc
FROM BookDocument;