oracle实现行转列功能,并使用逗号进行隔开拼接,成为一条数据

有两种方式

1、第一种:使用WM_CONCAT函数,不过这个函数已经被oracle弃用了,不建议使用,如果数据库中还有这个函数也可以使用

select sfc_no,wm_concat(mark_operation_id) from bp_marking where create_date>sysdate-1/24 group by sfc_no

简单说一下就是查询bp_marking表中的sfc_no与对应的所有的mark_operation_id的字段,并且合并到一列中

结果显示如下:

实现去重:就是把重复的去掉

直接加一个distinct即可

select sfc_no,wm_concat(distinct mark_operation_id) from bp_marking where create_date>sysdate-1/24 group by sfc_no

 

 

具体使用方式参考:https://www.cnblogs.com/yujin595/p/9829821.html

 

如果没有这个函数也想添加的话,可以试一下如下的方法(具体是否能用我没试过)

https://www.cnblogs.com/YuyuanNo1/p/7910714.html

 

 

2、第二种:使用LISTAGG函数

select sfc_no,LISTAGG(mark_operation_id,',') within group (order by mark_operation_id) from bp_marking where create_date>sysdate-1/24 group by sfc_no

结果跟上面的结果是一样的。

具体使用参考:https://blog.csdn.net/defonds/article/details/80455816

 如何实现去重:

把表再嵌套一层即可。即先把重复的数据去掉,然后再对这个表进行listagg操作。

select sfc_no,LISTAGG(mark_operation_id,',') within group (order by mark_operation_id) from (select distinct sfc_no,mark_operation_id from bp_marking where create_date>sysdate-1/24) group by sfc_no

 

执行完之后有时候会显示字符串连接过长的问题,因为listagg设定的字符串长度只有4000,超过4000就会报错。具体处理方法暂时也不清楚。

 

posted @ 2020-12-31 11:46  masha2017  阅读(4104)  评论(0编辑  收藏  举报