oracle中groupby和orderby混合使用(特殊情况处理)

需求:下面结果集中的记录,根据post字段分组,name字段值合并显示,同时根据item_sort排序。

 首先想到的是这样的:

这样写没有能够达到想要的效果,因为post字段为‘三级领导’的两条记录name字段并没有合并到一行。

于是请教了大佬,使用min函数对item_sort字段处理,这样就不需要在groupby中写item_sort字段,于是就有了下面的语句:

select t2.post,to_char(wm_concat(name)) name,min(item_sort) sort from (
      select t1.post,t1.item_sort,t3.name
      from t_organization_person_req t1
      left join T_SECURITY_PERSON t3 on t3.id=t1.cid
      where t1.org_id=886
   ) t2
 group by t2.POST
 order by sort

版权声明:本文为liangmengbk原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。