Lets assume we have a dataset with the following data:
timestamp,col1
1533286270,1
1533286271,2
1533286272,3
1533286273,4
1533286274,5
I want to get preceding col1
value in the col1_prev
to be able to compare them. The result should be the same as pandas.shift(-1)
.
How to achieve this functionality with plain SQL query?
Query result should look like this:
timestamp,col1,col1_prev
1533286270,1,NULL
1533286271,2,1
1533286272,3,2
1533286273,4,3
1533286274,5,4
Using the lag()
function is one way:
WITH
input AS (
SELECT
1533286270 AS timestamp,
1 AS col1
UNION ALL
SELECT
1533286271 AS timestamp,
2 AS col1
UNION ALL
SELECT
1533286272 AS timestamp,
3 AS col1
UNION ALL
SELECT
1533286273 AS timestamp,
4 AS col1
UNION ALL
SELECT
1533286274 AS timestamp,
5 AS col1 )
SELECT
timestamp,
col1,
LAG(col1) OVER(ORDER BY col1) AS col1_prev
FROM
input
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With