sydney
sydney

Reputation: 131

Combine rows into single column in Oracle

Does anyone know how to combine rows into a single column in Oracle? For example, say I have this table:

Customer   Bought
----------------------------
John       laptops
John       Phones
Lisa       Watches

I want a query to produce the following format:

Customer     CustomerBought
------------------------------------------
John         Laptops, Phones
Lisa         Watches

Thanks!

Upvotes: 0

Views: 48

Answers (1)

Patrick Marchand
Patrick Marchand

Reputation: 3445

with data_qry (name, item)
as
(select 'John', 'Laptop' from dual union all
 select 'John', 'Phone' from dual union all
 select 'Lisa', 'Watches' from dual union all
 select 'Lisa', 'Glasses' from dual
 )
 select name, listagg(item, ', ') within group (order by item) as items
 from data_qry
 group by name

Upvotes: 1

Related Questions