为什么SQLite LEAD总是返回NULL?

时间:2019-03-01 04:55:04

标签: sqlite window-functions

我正在跑步:

.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

我有下表:

sqlite> .version
SQLite 3.27.1 2019-02-08 13:17:39 0eca3dd3d38b31c92b49ca2d311128b74584714d9e7de895b1a6286ef959a1dd
zlib version 1.2.11
clang-10.0.0

据此填充:

CREATE TABLE FOO ( 
  ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
  SORT REAL NOT NULL UNIQUE DEFAULT 0, 
  NAME TEXT NOT NULL
);
CREATE TRIGGER FOO_sort_after_insert_trigger
  AFTER INSERT ON FOO FOR EACH ROW
    BEGIN
      UPDATE FOO
      SET SORT = (
        SELECT IFNULL(MAX(SORT), 0) + 1
        FROM FOO
      )
      WHERE ID = NEW.ID;
    END;

我正在尝试在行sqlite> select * from foo order by sort; ID|SORT|NAME 1|1.0|A 2|3.5|B 4|4.0|D 3|5.0|C 的{​​{1}}之后找到下一个sort值,但是我总是得到默认值,但是我希望得到2

sort

1 个答案:

答案 0 :(得分:1)

警告:SQLite window functions are only available in version 3.25.0 and later,如果您在Android上使用SQLite,则即使4.0也只有sqlite> select lead(sort, 1, -42) over (order by sort) as next_sort from foo where id = 2; next_sort -42 older Android versions have even older SQLites

SQLite window functions对结果集(而不是表格)进行操作。因此,我的问题是我的Android API 28子句。这将结果集限制为1行,因此SQLite 3.22.0始终返回默认值。为了实现我的目标,我必须在结果集中包含足够的结果,以使window函数可以访问下一行:

where id = 2