我有一个包含以下架构和示例值的表。
我正在尝试编写一个查询,为每个用户提供连接状态和断开状态的最大(时间戳)。
我正在寻找的所需查询的输出架构示例是:
用户名 |已连接_MAX_时间戳 | Disconnected_MAX_Timestamp
我试过的查询是这样的:
SELECT username,socketstatus,max(timestamp) from socketinfo group by username,socketstatus
同样的输出是:
谁能告诉我如何实现所需的输出?
最佳答案
只需使用条件聚合:
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/