oracle group by字符串拼接

2022/6/8 2:22:40

本文主要是介绍oracle group by字符串拼接,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

有一张表test,字段分别是主键ID,road_id,user_id,region,现在的需求是对road_id分组,然后将region字段用逗号拼接起来
test表:

分组的SQL如下:

SELECT road_id , LISTAGG(REGION, ',') WITHIN GROUP (ORDER BY road_id) region 
 FROM rasmp_test WHERE region IS NOT NULL GROUP BY road_id

分组后的结果:

发现region有重复的数字,需要对其进行去重
修改后的SQL如下:

SELECT road_id , LISTAGG(REGION, ',') WITHIN GROUP (ORDER BY road_id) region FROM (
	SELECT DISTINCT road_id, region FROM rasmp_test 
) t1 WHERE region IS NOT NULL GROUP BY road_id;

执行结果:



这篇关于oracle group by字符串拼接的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程