Oracle11gR2スキーマのセットアップ :
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
/
クエリ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 )
結果 :
| 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 |