gpt4 book ai didi

mysql - 如何根据另一个列值获取最大值(列值)

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:19 24 4
gpt4 key购买 nike

我有一个包含以下架构和示例值的表。

enter image description here

我正在尝试编写一个查询,为每个用户提供连接状态和断开状态的最大(时间戳)。

我正在寻找的所需查询的输出架构示例是:

用户名 |已连接_MAX_时间戳 | Disconnected_MAX_Timestamp

我试过的查询是这样的:

SELECT username,socketstatus,max(timestamp) from socketinfo group by username,socketstatus

同样的输出是:

enter image description here

谁能告诉我如何实现所需的输出?

最佳答案

只需使用条件聚合:

select username,
max(case when socketstatus = 'Connected' then timestamp end) as max_connected_timestamp,
max(case when socketstatus = 'Disconnected' then timestamp end) as max_disconnected_timestamp,
from socketinfo
group by username;

关于mysql - 如何根据另一个列值获取最大值(列值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47101864/

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