This question already has an answer here:
I have a table that looks like this:
A 1
A 2
B 1
B 2
And I want to produce a result set that looks like this:
A 1 2
B 1 2
Is there a SQL statement that will do this? I am using Oracle.
Related questions:
- Returning multiple rows from a single row My question is close to the opposite of this question.
- Use LINQ to concatenate This is exactly what I want to do, but without LINQ.
Try something like :
Freely inspired by an answer found in this Oracle forum.
EDIT: this solution proved very resources intensive with requests involving something like 105 rows. I ended up replacing this by custom aggregate functions as suggested by John.
very useful when used in pl/sql - can be casted to a user defined collection.
In
Oracle 10g+
:See this article in my blog for explanations:
GROUP_CONCAT
inOracle 10g
It depends on the version of Oracle you're using. If it supports the wm_concat() function, then you can simply do something like this:
wm_concat() basically works just like group_concat() in MySQL. It may not be documented, so fire up ye olde sqlplus and see if it's there.
If it isn't there, then you'll want to implement something equivalent yourself. You can find some instructions on how to do this in the string aggregation page at oracle-base.com.
If you have got 10g, then you have to go through the function below:
So, you can do like:
Fiddle here
If you have got oracle 11g, you can use listagg :
Fiddle here for Listagg
Pretty old topic, but it could help others since Oracle improved in the mean time.
The LISTAGG function is what you are looking for (in 11g at least)