vendredi 6 mars 2015

trying to a lag a column in SQLite

I have a table where rows represent individuals in a point in time. I am trying create a column (statusLag) with the lagged values of another column (status), like so:



workerID timeVar status statusLag
---------------------------------------------
1 1 0 NULL
1 2 1 0
1 3 1 1
1 4 1 1
----------------------------------------------
2 3 1 NULL
2 4 0 1
2 5 1 0
2 6 0 1


What I have tried


Doing the following fails (leaves the column unchanged), and I can't understand why



ALTER TABLE myTable ADD COLUMN statusLag INTEGER;

INSERT INTO myTable (statusLag)
SELECT
(SELECT t2.status
FROM myTable t2
WHERE t2.workerID = t1.workerID AND
t2.timeVar < t1.timeVar
ORDER BY t2.timeVar desc
LIMIT 1)
FROM myTable t1;


I almost achieve what I want by creating a new table:



CREATE TABLE myOtherTable
( 'workerID' REAL NOT NULL,
'timeVar' INTEGER NOT NULL,
'statusLag' INTEGER,
PRIMARY KEY('workerID', 'timeVar')
)

INSERT INTO myOtherTable
SELECT t1.workerID, t1.timeVar,
(SELECT t2.status
FROM myTable t2
WHERE t2.workerID = t1.workerID AND
t2.timeVar < t1.timeVar
ORDER BY t2.timeVar desc
LIMIT 1)
FROM myTable t1;


To achieve what I want, I would need to join myOtherTable with myTable. But this looks quite inefficient to me.


So my questions are: Why didn't my first attempt work? And How do I achieve what I want in a more efficient way?


Aucun commentaire:

Enregistrer un commentaire