If you're using a version of Oracle that doesn't support LISTAGG()
(e.g., Oracle 10g), then there are a couple of things you can do. The easiest is to use the undocumented WM_CONCAT()
function (which returns either a VARCHAR
or a CLOB
depending on the release):
SELECT reference_id, pallet_id, SUM(update_qty) as total qty
, WM_CONCAT(user_def_type_1)
FROM inventory_transaction
GROUP BY reference_id
One difficulty with using WM_CONCAT()
is that the results of the concatenation won't be ordered. You also have no choice about your delimiter. Another option, probably a better one, is to use XMLAGG()
(this is actually a documented function):
SELECT reference_id, pallet_id, SUM(update_qty) as total qty
, XMLAGG(XMLELEMENT(e, user_def_type_1 || ',')).EXTRACT('//text()')
FROM inventory_transaction
GROUP BY reference_id;
Here you have your choice of delimiters, and XMLAGG()
supports an ORDER BY
clause:
SELECT reference_id, pallet_id, SUM(update_qty) as total qty
, XMLAGG(XMLELEMENT(e, user_def_type_1 || ',') ORDER BY user_def_type_1).EXTRACT('//text()')
FROM inventory_transaction
GROUP BY reference_id;
You can find other options at this Stack Overflow question.