sql >> Database >  >> RDS >> Oracle

Selecteer subquery die meerdere rijen retourneert als tekenreeks (één kolom) in tabel (Oracle SQL)

SQL Fiddle

Oracle 11g R2 Schema-instellingen :

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 )

Resultaten :

| 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 |


  1. SQL juiste join

  2. Jij bent het niet, ik ben het (I/O-probleemoplossing)

  3. Krijg frequentieverdeling van een decimaal bereik in MySQL

  4. Panda's to_sql mislukt op dubbele primaire sleutel