I have SQL code below. If I removed , LAG(myNum, 1) OVER(ORDER BY id)
from the last SQL statement, my code would work. If I keep using LAG()
and OVER()
in the last SQL statement, I got error as Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY id) from db001.myTable001' at line 1
. Any ideas how to fix? Thanks!
DROP TABLE IF EXISTS db001.myTable001;
CREATE TABLE IF NOT EXISTS db001.myTable001
(id int, myDate VARCHAR(3), myNum int);
INSERT INTO db001.myTable001
(id, myDate, myNum)
VALUES
(1, 'Mon', 4),
(2, 'Tue', 6),
(3, 'Wed', 3);
SELECT * from db001.myTable001;
SELECT id, myDate, myNum as curNum, LAG(myNum, 1) OVER(ORDER BY id)
from db001.myTable001;