我是 Oracle SQL 的新手,希望有人可以帮助我。
我有一个使用Listagg组合多个列/值的Select语句。 SQL作业的输出是一个文本文件,然后手动导入Excel。
我的查询本身有效,但我希望在生成的文本文件中强制使用回车符/新行,这样我的Listagg中的所有数据都不会出现在一行中。
我在聚合的列之间尝试了|| ' chr(10) column name: ' ||
和|| ' CHAR(10) column name: ' ||
但是在导入文本文件时 Excel无法将其识别为新行(在同一单元格内)并将其显示为文本,并且全部在同一行中。
有人能告诉我如何实现这一目标吗?
更新
我的查询(第一部分):
SELECT
a.SHIPPER_ID
, LISTAGG
(
'Item No.: ' || ducsi.ITEM_NUMBER ||
' chr(10)Article: ' || c.ARTICLE ||
' chr(10)Quantity: ' || b.QUANTITY ||
' chr(10)chr(10)'
) WITHIN GROUP (ORDER BY b.SHIPMENT_ID)
/* ... */
当前输出:
Item No.: 123 chr(10)Article: Item A chr(10)Quantity: 10 ...
预期输出:
Item No.: 123
Article: Item A
Quantity: 10
...
非常感谢, 麦克
答案 0 :(得分:1)
您获取chr(10)
作为文字的原因是因为,您将其包含在文字字符串中。你必须将它连接到文本字符串,例如'some text'||chr(10)||'some more text'
但是,我认为listagg并不是你应该在这里使用的,特别是因为你可以在结果字符串中输出多少限制。
相反,我认为你是在UNPIVOT之后,例如:
WITH sample_data AS (SELECT 1 shipper_id, 10 item_number, 'abc' article, 100 quantity FROM dual UNION ALL
SELECT 2 shipper_id, 20 item_number, 'efg' article, 200 quantity FROM dual UNION ALL
SELECT 3 shipper_id, 30 item_number, 'hij' article, 300 quantity FROM dual)
-- end of mimicking a table called sample_data with data in it
-- see SQL below:
SELECT CASE WHEN row_number() OVER (PARTITION BY shid
ORDER BY CASE WHEN column_name = 'Item No.:' THEN 1
WHEN column_name = 'Article:' THEN 2
WHEN column_name = 'Quantity:' THEN 3
ELSE 4
END) = 1
THEN shid
END shipper_id,
column_name||' '||vals results
FROM (SELECT shipper_id shid,
to_char(item_number) "Item No.:",
article "Article:",
to_char(quantity) "Quantity:"
FROM sample_data)
UNPIVOT (vals FOR column_name IN ("Item No.:", "Article:", "Quantity:"))
ORDER BY shid,
CASE WHEN column_name = 'Item No.:' THEN 1
WHEN column_name = 'Article:' THEN 2
WHEN column_name = 'Quantity:' THEN 3
ELSE 4
END;
SHIPPER_ID RESULTS
---------- --------------------------------------------------
1 Item No.: 10
Article: abc
Quantity: 100
2 Item No.: 20
Article: efg
Quantity: 200
3 Item No.: 30
Article: hij
Quantity: 300
此处使用row_number()分析函数以确保仅为第一行显示shipper_id。
case when column_name = ...
案例表达式用于确保以正确的顺序(item_number,文章和数量)输出列(在row_number()分析函数和整体结果中)。
ETA:如果每个出货单ID可以有多个item_numbers,那么需要对上述查询进行一些调整:
WITH sample_data AS (SELECT 1 shipper_id, 10 item_number, 'abc' article, 100 quantity FROM dual UNION ALL
SELECT 1 shipper_id, 11 item_number, 'xyz' article, 110 quantity FROM dual UNION ALL
SELECT 2 shipper_id, 20 item_number, 'efg' article, 200 quantity FROM dual UNION ALL
SELECT 3 shipper_id, 30 item_number, 'hij' article, 300 quantity FROM dual)
-- end of mimicking a table called sample_data with data in it
-- see SQL below:
SELECT CASE WHEN row_number() OVER (PARTITION BY shid ORDER BY CASE WHEN column_name = 'Item No.:' THEN 1 WHEN column_name = 'Article:' THEN 2 WHEN column_name = 'Quantity:' THEN 3 ELSE 4 END) = 1
THEN shid
END shipper_id,
column_name||' '||vals results
FROM (SELECT shipper_id shid,
item_number,
to_char(item_number) "Item No.:",
article "Article:",
to_char(quantity) "Quantity:"
FROM sample_data)
UNPIVOT (vals FOR column_name IN ("Item No.:", "Article:", "Quantity:"))
ORDER BY shid,
item_number,
CASE WHEN column_name = 'Item No.:' THEN 1 WHEN column_name = 'Article:' THEN 2 WHEN column_name = 'Quantity:' THEN 3 ELSE 4 END;
SHIPPER_ID RESULTS
---------- --------------------------------------------------
1 Item No.: 10
Article: abc
Quantity: 100
Item No.: 11
Article: xyz
Quantity: 110
2 Item No.: 20
Article: efg
Quantity: 200
3 Item No.: 30
Article: hij
Quantity: 300