How to get multiple rows output in one line

oracleoracle-11g-r2

The data is like Jane has items pen pencil eraser.

If I write the statement 'select * from item'
the output is:

Jane pen
Jane pencil
Jane eraser
Jack box
Jack wiper

I want the output to be like:

Jane pen pencil eraser
Jack box wiper

How to do this?

Best Answer

You can try as below:

SELECT name,
LISTAGG(item_name, ' ') WITHIN GROUP (ORDER BY item_name) AS item_name 
FROM item 
GROUP BY name;