![](/img/trans.png)
[英]How to get multiple values of a column based on max value of another column?
[英]How to get max(column value) based on another column value
只需使用條件聚合:
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;
SELECT t1.username,
t1.socketstatus,
max(t1.timestamp) AS "Connected_MAX_Timestamp",
t2.username,
t2.socketstatus,
max(t2.timestamp) "Disconnected_MAX_Timestamp"
FROM socketinfo t1,
socketinfo t2
WHERE t1.socketstatus='Connected'
AND t2.socketstatus='Disconnected'
AND t1.username=t2.username
GROUP BY t1.username,
t1.socketstatus,
t2.username,
t2.socketstatus;
如果我正確理解了您的問題,則此方法適用於Postgres 9.5。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.