SQL LAG function over dates

Viewed 203

I have the following table (example):

+----+-------+-------------+----------------+
| id | value | last_update | ingestion_date |
+----+-------+-------------+----------------+
| 1  | 30    | 2021-02-03  | 2021-02-07     |
+----+-------+-------------+----------------+
| 1  | 29    | 2021-02-03  | 2021-02-06     |
+----+-------+-------------+----------------+
| 1  | 28    | 2021-01-25  | 2021-02-02     |
+----+-------+-------------+----------------+
| 1  | 25    | 2021-01-25  | 2021-02-01     |
+----+-------+-------------+----------------+
| 1  | 23    | 2021-01-20  | 2021-01-31     |
+----+-------+-------------+----------------+
| 1  | 20    | 2021-01-20  | 2021-01-30     |
+----+-------+-------------+----------------+
| 2  | 55    | 2021-02-03  | 2021-02-06     |
+----+-------+-------------+----------------+
| 2  | 50    | 2021-01-25  | 2021-02-02     |
+----+-------+-------------+----------------+

The result I need: It should be the last updated value in the column value and the penult value (based in the last_update and ingestion_date) in the value2.

+----+-------+-------------+----------------+--------+
| id | value | last_update | ingestion_date | value2 |
+----+-------+-------------+----------------+--------+
| 1  | 30    | 2021-02-03  | 2021-02-07     | 28     |
+----+-------+-------------+----------------+--------+
| 2  | 55    | 2021-02-03  | 2021-02-06     | 50     |
+----+-------+-------------+----------------+--------+

The query I have right now is the following:

SELECT id, value, last_update, ingestion_date, value2
FROM
(SELECT *,
        ROW_NUMBER() OVER(PARTITION BY id ORDER BY last_update DESC, ingestion_date DESC) AS order,
        LAG(value) OVER(PARTITION BY id ORDER BY last_update, ingestion_date) AS value2
    FROM table)
WHERE ordem = 1

The result I am getting:

+----+-------+-------------+----------------+--------+
| ID | value | last_update | ingestion_date | value2 |
+----+-------+-------------+----------------+--------+
| 1  | 30    | 2021-02-03  | 2021-02-07     | 29     |
+----+-------+-------------+----------------+--------+
| 2  | 55    | 2021-02-03  | 2021-02-06     | 50     |
+----+-------+-------------+----------------+--------+

Obs: I am using Athena from AWS

0 Answers
Related