gpt4 book ai didi

SQL获取最后的日期时间记录

转载 作者:行者123 更新时间:2023-12-01 19:03:52 25 4
gpt4 key购买 nike

我正在尝试从恰好存储多个状态的表中获取最后一个日期时间记录。我的 table 看起来像这样:

+---------+------------------------+-------+
|filename |Dates |Status |
+---------+------------------------+-------+
|abc.txt |2012-02-14 12:04:45.397 |Open |
|abc.txt |2012-02-14 12:14:20.997 |Closed |
|abc.txt |2013-02-14 12:20:59.407 |Open |
|dfg.txt |2012-02-14 12:14:20.997 |Closed |
|dfg.txt |2013-02-14 12:20:59.407 |Open |
+---------+------------------------+-------+

结果应该是

+---------+------------------------+-------+
|filename |Dates |Status |
+---------+------------------------+-------+
|abc.txt |2013-02-14 12:20:59.407 |Open |
|dfg.txt |2013-02-14 12:20:59.407 |Open |
+---------+------------------------+-------+

最佳答案

如果您希望每个文件名占一行,反射(reflect)特定状态并列出最近的日期,那么这就是您的 friend :

select filename ,
status ,
max_date = max( dates )
from some_table t
group by filename , status
having status = '<your-desired-status-here>'

简单!

关于SQL获取最后的日期时间记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16550703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com