SQLite从行向下一行添加计数器

时间:2014-01-24 00:19:11

标签: sqlite row add counter

我有下表:

Table final
-id INTEGER PRIMARY KEY
-addcounter TEXT NOT NULL
-secondaryID TEXT NOT NULL

使用以下格式:

1   0   1
2   1   null
3   1   null
4   0   null
5   0   null

所以基本上,on(行X的secondaryID)=(行X-1的secondaryID)+(行X-1的addcounter)

因此上表应该导致:

1   0   1
2   1   1
3   1   2
4   0   3
5   0   3

1 个答案:

答案 0 :(得分:1)

UPDATE final
SET secondaryID = (SELECT secondaryID
                   FROM final
                   ORDER BY id
                   LIMIT 1) +
                  (SELECT SUM(addcounter)
                   FROM final AS f2
                   WHERE id < final.id)
WHERE secondaryID IS NULL