我有下表。當我缺少 num 維度中的值時,我想用前一行的值替換它。以下是我嘗試過的示例。我正在使用滯后視窗功能,但無法正常作業。
CREATE TABLE test(dt text, num INT);
INSERT INTO test VALUES("2011-11-11", 3);
INSERT INTO test VALUES("2011-11-12", NULL);
INSERT INTO test VALUES("2011-11-13", 5);
INSERT INTO test VALUES("2011-11-14", NULL);
INSERT INTO test VALUES("2011-11-15", NULL);
INSERT INTO test VALUES("2011-11-16", 2);
select dt,
case when num is not null then num
else lag(num,1,0) over (order by dt) end
from test
我得到的錯誤:OperationalError: near "(": syntax error我什至不確定這意味著什么。任何幫助將不勝感激。
uj5u.com熱心網友回復:
Window 函式支持首次添加到 SQLite 的發布版本為 3.25.0 (2018-09-15)。您需要升級 SQLite 二進制檔案,或者不使用 Window 函式。見https://www.sqlite.org/windowfunctions.html#history
uj5u.com熱心網友回復:
SQLite 中的視窗函式僅在 3.25.0 版本中引入。作為一種解決方法,您可以使用相關子查詢:
select dt,
COALESCE(
num,
(SELECT num FROM test t2 WHERE t2.dt < t1.dt ORDER BY t2.dt DESC LIMIT 1)
) AS num_lag
FROM test t1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/519840.html
標籤:sqlite窗函数
