sqloracle-databasecollectionsoracle11gr2

Select from multiple PL/SQL collections of the same type


there are types

CREATE OR REPLACE TYPE DATE_RANGE IS OBJECT (
    STARTDATE DATE,
    ENDDATE DATE
);

CREATE OR REPLACE TYPE DATE_RANGES_T IS TABLE OF DATE_RANGE;

and table with collection per row

---------------------------------------------------------
    | COLUMN
---------------------------------------------------------
ROW | DATE_RANGES_T(
    |     DATE_RANGE(DATE '2021-01-01',DATE '2021-01-02'),
    |     DATE_RANGE(DATE '2021-01-03',DATE '2021-01-04'),
    |     DATE_RANGE(DATE '2021-01-05',DATE '2021-01-06')
    | )
---------------------------------------------------------
ROW | DATE_RANGES_T(
    |     DATE_RANGE(DATE '2021-01-07',DATE '2021-01-08'),
    |     DATE_RANGE(DATE '2021-01-09',DATE '2021-01-10')
    | )
---------------------------------------------------------
ROW | DATE_RANGES_T(
    |     DATE_RANGE(DATE '2021-01-11',DATE '2021-01-12')
    | )
---------------------------------------------------------

is it possible to select all date_range's objects from this table to resultset like that?

DATE_RANGE(DATE '2021-01-01',DATE '2021-01-02')
DATE_RANGE(DATE '2021-01-03',DATE '2021-01-04')
DATE_RANGE(DATE '2021-01-05',DATE '2021-01-06')
DATE_RANGE(DATE '2021-01-07',DATE '2021-01-08')
DATE_RANGE(DATE '2021-01-09',DATE '2021-01-10')
DATE_RANGE(DATE '2021-01-11',DATE '2021-01-12')

something like this

WITH TT AS (
    SELECT DATE_RANGES_T(
        DATE_RANGE(DATE '2021-01-01',DATE '2021-01-02'),
        DATE_RANGE(DATE '2021-01-03',DATE '2021-01-04'),
        DATE_RANGE(DATE '2021-01-05',DATE '2021-01-06')
    ) DT FROM DUAL
    UNION ALL
    SELECT DATE_RANGES_T(
        DATE_RANGE(DATE '2021-01-07',DATE '2021-01-08'),
        DATE_RANGE(DATE '2021-01-09',DATE '2021-01-10')
    )  FROM DUAL
    UNION ALL
    SELECT DATE_RANGES_T(
        DATE_RANGE(DATE '2021-01-11',DATE '2021-01-12')
    ) FROM DUAL
)
SELECT
    (SELECT STARTDATE FROM TABLE(DT)), (SELECT ENDDATE FROM TABLE(DT))
FROM TT;

but just with no ORA-01427: single-row subquery returns more than one row error occurs

in Oracle database 11.2


Solution

  • You need to use the TABLE() function as follows:

    WITH TT AS (
        SELECT DATE_RANGES_T(
            DATE_RANGE(DATE '2021-01-01',DATE '2021-01-02'),
            DATE_RANGE(DATE '2021-01-03',DATE '2021-01-04'),
            DATE_RANGE(DATE '2021-01-05',DATE '2021-01-06')
        ) DT FROM DUAL
        UNION ALL
        SELECT DATE_RANGES_T(
            DATE_RANGE(DATE '2021-01-07',DATE '2021-01-08'),
            DATE_RANGE(DATE '2021-01-09',DATE '2021-01-10')
        )  FROM DUAL
        UNION ALL
        SELECT DATE_RANGES_T(
            DATE_RANGE(DATE '2021-01-11',DATE '2021-01-12')
        ) FROM DUAL
    )
    SELECT T.STARTDATE, T.ENDDATE FROM TT, TABLE(TT.DT) T;
    

    db<>fiddle