I'm trying to convert a single-columned subquery into a command-separated VARCHAR
-typed list of values.
This is identical to this question, but for Oracle rather than SQL Server or MySQL.
You can use listagg() to convert rows into a comma separated string.
I found this that seems to work. Thoughts?
SELECT SUBSTR (c, 2) concatenated
FROM (SELECT SYS_CONNECT_BY_PATH ( myfield, ',') c, r
FROM (SELECT ROWNUM ID, myfield,
RANK () OVER (ORDER BY ROWID DESC) r
FROM mytable
ORDER BY myfield)
START WITH ID = 1
CONNECT BY PRIOR ID = ID - 1)
WHERE r = 1;
There is an excellent summary of the available string aggregation techniques on Tim Hall's site.
11.2 introduced LISTAGG, which unlike WM_CONCAT is documented. We are not on 11.2 yet, so we use a custom aggregate function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With