SQL Project Planning

  • + 0 comments

    ORACLE:

    WITH t1 AS (
        SELECT CONNECT_BY_ROOT(start_date) s_date, end_date e_date, level lvl
        FROM Projects
        WHERE CONNECT_BY_ISLEAF = 1
        CONNECT BY NOCYCLE PRIOR end_date = start_date
    ),
    t2 AS (
        SELECT s_date, e_date,
            ROW_NUMBER() OVER (PARTITION BY e_date ORDER BY lvl DESC) AS rn
        FROM t1
    )
    SELECT s_date, e_date
    FROM t2
    WHERE rn = 1
    ORDER BY (e_date-s_date), s_date
    ;