我正在尝试查询oracle数据库并在结果的单个单元格中获取名称列表。
我有一个类似以下的查询,它在单独的行中产生名称:
SELECT (lastname || ', ' || firstname) as fullname
FROM users
WHERE {some condition}
-- result --
fullname
Anderson, Alex
Baker, Bob
Clark, Carl
但是,当我尝试使用LISTAGG连接这些行时,出现以下错误:
SELECT LISTAGG(fullname, '; ') WITHIN GROUP (ORDER BY fullname) as instructors
FROM
(
SELECT (lastname || ', ' || firstname) as fullname
FROM users
WHERE {some condition}
)
-- desired result --
instructors
Anderson, Alex; Baker, Bob; Clark, Carl
-- actual result --
Error Type: System.Xml.XmlException
Error Message: hexadecimal value 0x00, is an invalid character. Line 1, position 32.
请注意,如果我在将INT值串联而成的字符串上使用LISTAGG,查询将按预期工作:
SELECT LISTAGG(fullname, '; ') WITHIN GROUP (ORDER BY fullname) as instructors
FROM
(
SELECT (pk1 || ', ' || pk1) as fullname -- pk1 is the users primary key
FROM users
WHERE {some condition}
)
-- result --
instructors
01, 01; 02, 02; 03, 03
我怀疑问题在于名字和姓氏字段以空字符结尾,并且LISTAGG无法从结果中正确删除空字符。我不确定如何从内部查询的结果中删除空字符。
请注意,这是客户的数据库,不允许修改其内容。我需要能够以其当前格式读取字段并产生所需的输出。
答案 0 :(得分:0)
我有同样的问题。对我来说,解决方案是:
SELECT replace(LISTAGG(fullname, '; ') WITHIN GROUP (ORDER BY fullname), chr(0)) as instructors