oracle sql 按某个字段分组然后从每组取出最大的一条纪录

时间:2022-06-14 13:37:55


--找到每个伙伴商户最多的地区,作为伙伴的地区

其实就是找出每组数据中,其中一列的值最大的,显示出来。
select s.agent_name,cc.city_name as province,CC1.CITY_NAME as city from (
select aa.agent_name,ci.province,ci.city,
ROW_NUMBER() OVER (partition by aa.agent_name ORDER BY count(user_id) desc) R from cust_info ci
join account_agent aa on CI.AGENT_ID=AA.AGENT_ID
group by aa.agent_name,ci.province,ci.city
order by aa.agent_name
)s join city_code cc on s.province = cc.city_code
join city_code cc1 on s.city = cc1.city_code where s.R<=1;



row_number() over( partition by 分组字段 order by 排序字段 desc