我们往往需要对一张表做group by操作,然后在每个组里通过聚合函数获取想要的信息。但是,如果需要同时对不同的列使用两个或者多个聚合函数,那么很可能结果不是我们想要的那样。本文就是讲述这种情况时的解决方法——子查询。比如,“一张表中,获取id_city相同时,height值最大的gid”。
应对上面的问题,可以使用自表的inner join查询,如下:
select gid
from tile_building_incity_new a
inner join
(select id_city,max(height) as height
from tile_building_incity_new
group by id_city) b
on a.id_city = b.id_city
and a.height = b.height