2

I have built a command that uses the BREAK ON command to stop the output of duplicate field names. For example:

f.name | f.value
f.name | f.value
f.name | f.value

becomes:

f.name | f.value
       | f.value
       | f.value

Is there any way to have this output as:

f.name | f.value,f.value,f.value

In some instances the f.name field with have over 20 f.values associated with it.

The output will eventually be used to import into other places so I am trying to make the output as friendly as possible.

4

1 回答 1

2

您不是在寻找 SQL*Plus 命令,而是在寻找字符串聚合。

假设您当前的查询是:

select name, value from my_table

您可以按如下方式更改它以获得您想要的结果。包含 DISTINCT 是为了消除列表中的重复结果。

select name, listagg(value, ', ') within group (order by value) as value
  from ( select distinct name, value from my_table )
 group by name

LISTAGG()仅在 11.2 中发布,如果您使用的是早期版本的 Oracle,您可以使用未记录的函数 WM_CONCAT() 或用户定义的函数 STRAGG(),如字符串聚合技术的有用页面中所述。

于 2013-03-16T13:28:55.747 回答