简体   繁体   中英

Oracle SQL Developer - Concat Data

Good Day,

I have a table which has a column which contains numbers only. However due its containing rule data for special IDs it giving back the result in more lines, for example:

you have a bookstore DB where all the costumers have an ID and the books also have their own ID. When you wish to know that how many books have been ordered by each costumer where the result is bugger than 1 it will appear in multiple lines, where the ID of the costumer is the same but in every line the books' IDs are different.

My question is:

What is the proper syntax, or code portion to use to get one line only for each and every costumer, where the book IDs are separated by commas in the same column?

I've tried like this:

BOOK_CONT AS (
SELECT DISTINCT BOOK_ID, LISTAGG(' ('||BOOK_ID||')', '; ')WITHIN GROUP (ORDER BY BOOK_ID) AS BOOKS
FROM BOOK_LIST)

You would use group_by and listagg() . Assuming that your table has columns customer_id and book_id , you would go:

select
    customer_id,
    listagg(book_id, ', ') within group(order by book_id) all_book_ids
from book_list
group by customer_id

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