Reputation: 131
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
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