This is a sample table data
Fruit Number
Apple 1
Apple 2
Apple 3
Kiwi 6
Kiwi 10
I try to concatenate the table column values to get the following
Fruit Number
Apple 1-2-3
Kiwi 6-10
Is there a way to query this or store procedure? Something like Concatenate over(partition by) , I don't know much about stored procedures. Thanks!
You don't need a stored procedure for that. Use
listagg
function:If your database version is 10g it is still simple: use
WM_CONCAT
(if doesn't recognize the name probably tryWMSYS.WM_CONCAT
) function, see example in this answer. Just in case you need to use separator which is different from,
, you can wrap the result inreplace
function; if you want to order the result just pre-order it in a subquery, e.g.:If you for some mysterious reason don't have the function in your instance you can use a polyfill, see my answer here.
OP is on Oracle 10g, and LISTAGG was introduced in 11g Release 2.
Therefore, in Oracle version prior to 11g where
LISTAGG
is not supported, you could use ROW_NUMBER() and SYS_CONNECT_BY_PATH functions.NOTE
Never use
WM_CONCAT
since it is an undocumented feature and it has been removed from 12c version.Any application which has had been relying on
wm_concat
function will not work once upgraded to12c
. Since, it has been removed. See Why not use WM_CONCAT function in Oracle?You will receive an “invalid identifier” error:
Therefore, there is no point relying on an undocumented feature which is no more made available in latest versions.