Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在Oracle 12c中将列连接到多行上_Sql_Oracle_Oracle12c - Fatal编程技术网

Sql 在Oracle 12c中将列连接到多行上

Sql 在Oracle 12c中将列连接到多行上,sql,oracle,oracle12c,Sql,Oracle,Oracle12c,我有一个关于Oracle12c中旧终端系统订单的注释表。每个订单参考都有几行注释,按序号排序 我想将每个订单引用的所有相关注释连接在一起,以便尝试从中提取一些地址数据。地址数据可以分布在几个不同的序列号上。结构如下: | SEQ | NOTE_TEXT | ORDER | ... | |-----|--------------------------|-------|-----| | 1 | The address for this | |

我有一个关于Oracle12c中旧终端系统订单的注释表。每个订单参考都有几行注释,按序号排序

我想将每个订单引用的所有相关注释连接在一起,以便尝试从中提取一些地址数据。地址数据可以分布在几个不同的序列号上。结构如下:

| 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中的保留关键字。最好使用其他标识符或使用
来转义它。

您可以使用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中是一个保留关键字。最好使用其他标识符或使用
来转义它。

OP没有在模拟输入的
order
列中显示值,但它们可能已经类似于
ABC123
;1、2、3不是序列号。因此,
SELECT
中的第一列可以是
“order”
。(很高兴你指出了甲骨文保留字的误用!)谢谢,古尔夫!是的,顶部的模拟输入应该有实际的订单号,而不是空白:P幸运的是,把表格放在一起的人使用了一个比“订单”更神秘的列名,是我试图简化事情-糟糕的lol。OP在模拟输入中没有显示
订单
列中的值,但它们很可能已经与ABC123相似了;1、2、3不是序列号。因此,
SELECT
中的第一列可以是
“order”
。(很高兴你指出了甲骨文保留字的误用!)谢谢,古尔夫!是的,顶部的模拟输入应该有实际的订单号,而不是空白:P幸运的是,把表格放在一起的人使用了一个比“订单”更神秘的列名,是我试图简化事情——糟糕的lol。