2

I have a table of items stored this way :

A1 | B1
A1 | B2
A1 | B3
A2 | B1
A2 | B4
...

And I need to retrieve with a SQL Query :

A1 | B1, B2, B3
A2 | B1, B4
...
1

2 Answers 2

3

If you have 11g Release 2 you can use Listagg:

Select a, Listagg(b, ', ') Within Group ( Order By b )
From t
Group By a

It allows to sort your values, and it already comes with Oracle:

A1  B1, B2, B3
A2  B1, B4

Otherwise you can use the stragg function by Tom Kyte, described in Rows to String.

Select a, stragg(b)
From t
Group By a

returns

A1  B1,B3,B2
A2  B1,B4
Sign up to request clarification or add additional context in comments.

2 Comments

Peter, if the OP was on 11gR2 could they use LISTAGG?
Unfortunately, I do not have 11g
-2
SELECT *
FROM `table1`
ORDER BY `afield` ASC

Use like this

1 Comment

Sorry, but I fail to see how this would help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.