【问题标题】:SQL Query to find the last event in a particular monthSQL 查询以查找特定月份中的最后一个事件
【发布时间】:2021-11-08 18:08:12
【问题描述】:

我正在尝试查询最后一个条目位于 1 月内(01/01/2020 至 31/01/2020)的 ID。

数据如下

ID  DATE
 
123 25/01/2020
123 27/01/2020
123 30/01/2020
123 02/02/2020
456 17/01/2020
456 18/01/2020
456 19/01/2020
456 22/01/2020
789 30/01/2020
789 01/01/2020
654 03/01/2020
654 08/01/2020
654 10/01/2020
654 25/01/2020

预期输出

ID  DATE

456 22/01/2020
654 25/01/2020

谢谢

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: sql date teradata


【解决方案1】:

您可以使用group byhaving

select id, max(date)
from t
group by id
having max(date) >= date '2020-01-01' and
       max(date) < date '2020-02-01'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多