'uma lista dentro do select no sql oracle

I would like to know if anyone can help me. I wanted to know if it is possible to do a select and in one of the lines have a list. That is, I have a table of orders that I'm pulling all the products and I wanted one of the columns to contain all the orders that have this item. Is it possible in slq oracle?



Solution 1:[1]

This is a job for LISTAGG().

Something like this might do it to get the products in each order.

SELECT order_id,
       LISTAGG(product, ', ') WITHIN GROUP (ORDER BY product) "products"
  FROM orders
 GROUP BY order_id
 ORDER BY order_id;

This might get the orders for each product.

SELECT product,
       LISTAGG(order_id, ', ') WITHIN GROUP (ORDER BY order_id) "orders"
  FROM orders
 GROUP BY product
 ORDER BY product;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 O. Jones