Select subquery that return multiple rows as string (one column) in table (Oracle SQL)

和自甴很熟 提交于 2020-01-06 06:54:41

问题


Example I have Product Data:

Product_No  Column1 Column2 ... ColumnX
1           A       10          
2           B       11
3           C       12

And for column X I need single data row from this table Inventory:

Product_No Inventory_No ColumnA ColumnB ColumnC
1           1           ABC     20      30
1           2           DDD     30      50
2           1           EFG     60      70
2           2           CDE     99      100
3           3           EFF     120     30

And the result for column x should be

Product_No  Column1 Column2 ... ColumnX
1           A       10          ABC-20-30,DDD-30-50
2           B       11          EFG-60-70,CDE-99-100
3           C       12          EFF-120-30

How to return that value without altering the main query join and from, i need sub-query to return that value. I have try list_aggregate but it is only possible for one column i need combine from multiple columns. Thank you.


回答1:


SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE Product ( Product_No, Column1, Column2 ) AS
SELECT 1, 'A', 10 FROM DUAL UNION ALL
SELECT 2, 'B', 11 FROM DUAL UNION ALL
SELECT 3, 'C', 12 FROM DUAL
/
CREATE TABLE Inventory ( Product_No, Inventory_No, ColumnA, ColumnB, ColumnC ) AS
SELECT 1, 1, 'ABC',  20,  30 FROM DUAL UNION ALL
SELECT 1, 2, 'DDD',  30,  50 FROM DUAL UNION ALL
SELECT 2, 1, 'EFG',  60,  70 FROM DUAL UNION ALL
SELECT 2, 2, 'CDE',  99, 100 FROM DUAL UNION ALL
SELECT 3, 3, 'EFF', 120,  30 FROM DUAL
/

Query 1:

SELECT p.*, i.ColumnX
FROM   Product p
       LEFT OUTER JOIN
       ( SELECT Product_no,
                LISTAGG(
                  ColumnA || '-' || ColumnB || '-' || ColumnC,
                  ','
                ) WITHIN GROUP ( ORDER BY Inventory_no )
                AS ColumnX
         FROM   Inventory
         GROUP BY Product_No
       ) i
       ON ( p.product_no = i.product_no )

Results:

| PRODUCT_NO | COLUMN1 | COLUMN2 |              COLUMNX |
|------------|---------|---------|----------------------|
|          1 |       A |      10 |  ABC-20-30,DDD-30-50 |
|          2 |       B |      11 | EFG-60-70,CDE-99-100 |
|          3 |       C |      12 |           EFF-120-30 |



回答2:


LISTAGG is for you.

Something along this:

 SELECT Product_No, LISTAGG(ColumnA || '-' || ColumnB || '-' || ColumnC, ',')
   FROM tbl
  GROUP BY Product_No;

More about LISTAGG: http://modern-sql.com/feature/listagg




回答3:


You bring the tables together using join. Then listagg() does the work of making column x:

select p.Product_No, p.Column1, p.Column2,
       listagg(i.ColumnA || '-' || i.ColumnB || '-' || i.ColumnC, ',') within group (order by inventory_no) as x
from product p left join
     inventory i
     on p.product_no = i.product_no
group by p.Product_No, p.Column1, p.Column2;


来源:https://stackoverflow.com/questions/46686205/select-subquery-that-return-multiple-rows-as-string-one-column-in-table-oracl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!