将多行连接到一行

时间:2019-11-07 12:16:34

标签: sql sql-server string-aggregation

我有以下数据,我必须将长文本列连接起来并使其成为一行。质询部分只有一行具有通知号,其他行为空。您无法按通知号分组。

enter image description here

我需要将输出作为2行

row number  Notification Number Plant   Creation Date   Language    Lineno  Tag Long Text
1           10014354914         A057    43466            EN            1     >X  aaabbbcccdddeeefffggghhhjjjkkklll
2           10014354915         A057    43466            EN            1     >X  aaabbbcccdddeeefffgggpppqqqrrrsss

我为此使用了光标。但这要花很多时间。

1 个答案:

答案 0 :(得分:0)

如果您使用的是oracle:

with data("row number", "Notification Number","Plant","Creation Date","Language","Lineno","Tag","Long Text") as (
select 1,10014354914,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffggghhhjjjkkklll' from dual
union all
select 2,10014354915,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffgggpppqqqrrrsss' from dual)
select LISTAGG("Long Text",'') within group (order by "row number") from data;

如果您使用的是ms-sql,请尝试以下操作:

SELECT u.[Long Text]  AS [text()]
            FROM yourtable u
            ORDER BY u.[row number]
            FOR XML PATH ('')