简体   繁体   中英

Sql query to combine multiple rows

I have a table as follows

|Cutomers|Orders|Items| |Cutomer1|Order1|Item1| |Cutomer1|Order1|Item2| |Cutomer1|Order1|Item3| |Cutomer1|Order2|Item1| |Cutomer1|Order2|Item3| |Cutomer1|Order2|Item4| |Cutomer2|Order1|Item6| | . | . | . | | . | . | . | | . | . | . |

I want to have the following table, seems like easy but i have no clue how to deal with it.

|Customers|Items| |Customer1|Item1| |Customer1|Item2| |Customer1|Item3| |Customer1|Item4| |Customer2|Item6| | . | . | | . | . | | . | . |

Any suggestions are very much appreciated!

I think this would suffice

select  distinct Customers, Items
from    YourTable

From Oracle 11gR2, the LISTAGG clause should do the trick:

SELECT customers,
       LISTAGG(items, ',') WITHIN GROUP (ORDER BY items)
FROM YOUR_TABLE
GROUP BY customers;

try this, it might work

you can also wm_concat

SELECT customers, wm_concat(items) as item
FROM   table
GROUP BY customers;

it might also work

for my sql,

select  distinct customers, items from tablename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM