我需要在listagg中插入一个char(10),但是它不能按预期工作。
这是我写的脚本:
select ID, listagg(value , ';')within group (order by ID) as "value"
from DTA
group by ID
Current Result
ID |value |
----|-----------|
1 |aaa;bbb;ccc|
2 |abc |
3 |abe |
Desired Result
ID |value
----|------
1 |aaa
|bbb
|ccc
----|------
2 |abc
----|------
3 |abe
我尝试使用它,但是没有按预期工作。
select ID, listagg(value , chr(10))within group (order by ID) as "value"
from DTA
group by ID
如何改善我的脚本?
答案 0 :(得分:1)
您的代码正确。您需要一个能够显示换行符的工具:
WITH DTA AS (
SELECT 1 AS ID, 'A' AS Value FROM dual
UNION ALL
SELECT 1 AS ID, 'B' FROM dual
)
select ID, listagg(value , chr(10))within group (order by ID) as "value"
from DTA
group by ID;
输出:
ID value
1 A
B
答案 1 :(得分:0)
在下面的查询中使用PIVOT进行尝试,很难通过listagg实现chr(10)。
WITH sample_data AS (SELECT 1 ID, 'aaa' value FROM dual UNION ALL
SELECT 1 ID, 'bbb' value FROM dual UNION ALL
SELECT 1 ID, 'ccc' value FROM dual UNION ALL
SELECT 2 ID, 'abc' value FROM dual UNION ALL
SELECT 3 ID, 'abe' value FROM dual )
SELECT CASE WHEN row_number() OVER (PARTITION BY DTA_ID
ORDER BY CASE
WHEN column_name = 'VALUE' THEN 1
END) = 1
THEN DTA_ID
END "Current ID",
vals "Result Value"
FROM (SELECT ID DTA_ID,
VALUE
FROM sample_data)
UNPIVOT (vals FOR column_name IN ("VALUE"))
ORDER BY DTA_ID;
答案 2 :(得分:0)
这可能会帮助您满足要求
select ID, listagg(value , || CHR (10)) within group (order by ID) as "value"