Create a SQLite view where a row depends on the previous row

前端 未结 4 826
猫巷女王i
猫巷女王i 2020-12-31 20:21

I\'d like to create a view in SQLite where a field in one row depends on the value of a field in the previous row. I could do this in Oracle using the LAG analy

4条回答
  •  礼貌的吻别
    2020-12-31 21:16

    Oracle equivalent is correct. Starting from SQLite 3.25.0 you could use LAG natively:

    WITH mytable(ITEM,DAY,PRICE) AS (
        VALUES
        ('apple',  CAST('20110107' AS DATE),    1.25),
        ('orange', CAST('20110102' AS DATE),    1.00),
        ('apple',  CAST('20110101' AS DATE),    1.00),
        ('orange', CAST('20110103' AS DATE),    2.00),
        ('apple',  CAST('20110108' AS DATE),    2.00),
        ('apple',  CAST('20110110' AS DATE),    1.50)
    )
    SELECT day, price, price-LAG(price) OVER (ORDER BY day) AS change
    FROM mytable
    WHERE item = 'apple'
    ORDER BY DAY;
    

提交回复
热议问题