![](/img/trans.png)
[英]How to retrieve the value which corresponding to the max in an other column in SQL?
[英]retrieve max value for the other column
我只需要为每个machine_id获得最大时间。 我的代码为每个machine_id检索所有时间值。 波纹管是我的代码产生的一些行
machine_id time
--------------------------
4246147567 2506135493517
1301977 2503322826186
4135091837 2498530284226
4246147567 2497077644943
4820021252 2496367903730
1301977 2495450309333
如您所见,我对machine_id(4246147567,1301977)的结果有所不同,它假设每台计算机的时间最长。 换句话说,我必须为每台机器有一个记录。
我当前的代码是:
select distinct
machine_id, time
from
failure_host_machine_events
where
event_type = 1
-- group by machine_id, time
order by
time desc
您应该使用max()并分组
select machine_id, max(time)
from failure_host_machine_events
where event_type = 1
group by machine_id
order by max(time) desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.