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就會報錯。具體處理方法暫時也不清楚。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM