需求:下面结果集中的记录,根据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