max函数
max函数是针对group by的每一个子集(相同值则为一个子集),筛选出最大值后与原数据合并 比如下面sql的含义是:以os_ip分组后,在每一个分组中筛选出updated_at最大的值,并与分组数据合并
SELECT job_id,ins_id,os_ip, max(updated_at) from job_result_info where ins_id like "auto%" and os_ip='10.203.178.176' group by os_ip
结果:
1046154 autocreate:0e806c37-ca1a-43d5-ba22-38e4d99cf657 10.203.178.176 **2019-09-27 10:34:11**
再看下1046154这条记录对应的updated_at数值
SELECT job_id,ins_id,os_ip, updated_at from job_result_info where job_id=1046154
结果:
1046154 autocreate:0e806c37-ca1a-43d5-ba22-38e4d99cf657 10.203.178.176 **2019-07-12 11:44:45**
也就是说,max函数筛选出来(updated_at)的值可能并不是该条记录(updated_at)的值,而是筛选出最大值后合并而成。