This following code should produce the same values for columns lag
and lag2
:
CREATE TABLE bug1 (
id INT,
value INT
);
INSERT INTO bug1 VALUES (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3);
SELECT
id,
value,
(LAG(value) OVER (PARTITION BY id ORDER BY value)) lag,
NULLIF((LAG(value) OVER (PARTITION by id ORDER BY value)), 0) lag2
FROM bug1
ORDER BY id, value;
Indeed, running it on PostgreSQL 9 produces the expected output:
id v lag lag2
1 1
1 2 1 1
1 3 2 2
2 1
2 2 1 1
2 3 2 2
But, running it on Redshift produces incorrect results:
id v lag lag2
1 1 2
1 2 1 3
1 3 2
2 1 2
2 2 1 3
2 3 2
Is this expected? I asked on AWS forums but no reply.