针对日期的 SQL LAG 函数

发布于 2025-01-13 08:47:34 字数 2190 浏览 1 评论 0原文

我有下表(示例):

+----+-------+-------------+----------------+
| 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     |
+----+-------+-------------+----------------+

我需要的结果: 它应该是列值中的最后更新值和值2中的倒数值(基于last_update和ingestion_date)。

+----+-------+-------------+----------------+--------+
| 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     |
+----+-------+-------------+----------------+--------+

我现在的查询如下:

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

我得到的结果:

+----+-------+-------------+----------------+--------+
| 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文