Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform pandas column shift in BigQuery?

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
like image 778
Alex T Avatar asked Oct 19 '25 14:10

Alex T


1 Answers

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

enter image description here

like image 194
Graham Polley Avatar answered Oct 22 '25 05:10

Graham Polley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!