[英]Concatenating a column over multiple rows in Oracle 12c
我有一張與Oracle 12c中的舊終端系統的訂單相關的注釋表。 每個訂單參考都有幾行注釋,按序號排序。
我想將每個訂單參考的所有相關注釋連接在一起,以便我可以嘗試從中提取一些地址數據。 地址數據可以分布在幾個不同的序列號上。 結構為:
| SEQ | NOTE_TEXT | ORDER | ... |
|-----|--------------------------|-------|-----|
| 1 | The address for this | | |
| 2 | is 123 The Street, City, | | |
| 3 | County, Postcode | | |
| 1 | This customer has ordered| | |
| 2 | this product on date | | |
| 1 | Some other note | | |
| 1 | This order is for A Smith| | |
| 2 | The address is 4 The Lane| | |
| 3 | City, County, Postcode | | |
------------------------------------------------
我想將其轉換為:
|--------|---------------------------------------------------------------------------|
| ORDER | NOTE_TEXT |
|--------|---------------------------------------------------------------------------|
| ABC123 | The address for this is 123 The Street, City, County, Postcode |
| DEF456 | This customer has ordered this product on date |
| GHI789 | Some other note |
| JKL012 | This order is for A Smith The address is 4 A Lane, City, County, Postcode |
|--------|---------------------------------------------------------------------------|
在連接之前修剪每個音符行可能會很好,但是我還需要確保在兩行的連接之間放置一個空格,以防萬一有人用文本填充了整個行。 哦,順序不對,所以我也需要先排序。
謝謝你的幫助!
您可以為此使用listagg:
select "order" || listagg(seq, '') within group (order by seq) as "order",
listagg(trim(note_text), ' ') within group (order by seq) as note_text
from your_table
group by "order";
另外,請注意, order
是oracle中的保留關鍵字。 最好使用其他一些標識符或使用"
將其轉義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.