Рассмотрим фактический агрегатный запрос с GROUP BY
, особенно с использованием Oracle LISTAGG
для значений через запятую в source_code и form_code .Выполните весь оператор ниже в вызове запроса R:
WITH wst_list AS (
SELECT Tons, "Year", "Name", "ID", primary_naics,
LISTAGG(WASTEW, ', ') WITHIN GROUP (ORDER BY "ID") AS WASTEW,
Lat, Lon
FROM (SELECT DISTINCT Tons, "Year", "Name", "ID", WASTEW, primary_naics, Lat, Lon
FROM "REPORTING")
GROUP BY Tons, "Year", "Name", "ID", primary_naics, Lat, Lon
),
src_list AS (
SELECT Tons, "Year", "Name", "ID", primary_naics,
LISTAGG(SOURCE_CODE, ', ') WITHIN GROUP (ORDER BY "ID") AS SOURCE_CODE,
Lat, Lon
FROM (SELECT DISTINCT Tons, "Year", "Name", "ID", SOURCE_CODE, primary_naics, Lat, Lon
FROM "REPORTING")
GROUP BY Tons, "Year", "Name", "ID", primary_naics, Lat, Lon
),
frm_list AS (
SELECT Tons, "Year", "Name", "ID", primary_naics,
LISTAGG(FORM_CODE, ', ') WITHIN GROUP (ORDER BY "ID") AS FORM_CODE,
Lat, Lon
FROM (SELECT DISTINCT Tons, "Year", "Name", "ID", FORM_CODE, primary_naics, Lat, Lon
FROM "REPORTING")
GROUP BY Tons, "Year", "Name", "ID", primary_naics, Lat, Lon
)
SELECT *
FROM wst_list
NATURAL JOIN src_list
NATURAL JOIN frm_list
Вывод
╔═══╤══════╤══════╤══════════╤══════════════╤═══════════════╤═══════════╤═══════════╤════════╤════════════════════╤════════════════════════╗
║ │ TONS │ Year │ Name │ ID │ PRIMARY_NAICS │ LAT │ LON │ WASTEW │ SOURCE_CODE │ FORM_CODE ║
╠═══╪══════╪══════╪══════════╪══════════════╪═══════════════╪═══════════╪═══════════╪════════╪════════════════════╪════════════════════════╣
║ 1 │ 0 │ 2017 │ Century │ NVD980895338 │ 562211 │ 39,61028 │ -119,2031 │ N, Y │ G01, G02, G03, G04 │ W001, W002, W004 ║
╟───┼──────┼──────┼──────────┼──────────────┼───────────────┼───────────┼───────────┼────────┼────────────────────┼────────────────────────╢
║ 2 │ 0 │ 2017 │ Airborne │ TN5210020140 │ 928110 │ 36,611123 │ -87,46234 │ N, Y │ G11, G13, G14, G32 │ W101, W200, W310, W801 ║
╚═══╧══════╧══════╧══════════╧══════════════╧═══════════════╧═══════════╧═══════════╧════════╧════════════════════╧════════════════════════╝
Rextester Demo