Ниже приведен пример запроса.
WITH t AS (
SELECT *
FROM (
SELECT 'Planner 2' AS planner_id, '15' AS assigned, '10' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '26%' AS average, '1' AS working_days, '2018.11.26' AS dates FROM dual UNION ALL
SELECT 'Planner 2' AS planner_id, '18' AS assigned, '12' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '21%' AS average, '1' AS working_days, '2018.11.21' AS dates FROM dual UNION ALL
SELECT 'Planner 2' AS planner_id, '16' AS assigned, '13' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '25%' AS average, '1' AS working_days, '2018.11.25' AS dates FROM dual UNION ALL
SELECT 'Planner 2' AS planner_id, '15' AS assigned, '14' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '28%' AS average, '1' AS working_days, '2018.11.28' AS dates FROM dual
UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '1' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '26%' AS average, '1' AS working_days, '2018.11.26' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '28' AS assigned, '2' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '21%' AS average, '1' AS working_days, '2018.11.21' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '26' AS assigned, '3' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '25%' AS average, '1' AS working_days, '2018.11.25' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '4' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '28%' AS average, '1' AS working_days, '2018.11.28' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '5' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '27%' AS average, '1' AS working_days, '2018.11.27' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '26' AS assigned, '6' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '24%' AS average, '1' AS working_days, '2018.11.24' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '7' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '29%' AS average, '1' AS working_days, '2018.11.29' AS dates FROM dual
)
)
SELECT *
FROM (
SELECT planner_id, projects, dates, quantity
FROM t
UNPIVOT (quantity FOR projects IN (assigned AS 'ASSIGNED', todo AS 'TODO', done AS 'DONE', rejected AS 'REJECTED', blocking AS 'BLOCKING', average AS 'AVERAGE', working_days AS 'WORKING_DAYS'))
)
PIVOT (MAX(quantity) FOR (dates) IN ('2018.11.26' AS "2018.11.26" , '2018.11.21' AS "2018.11.21" , '2018.11.25' AS "2018.11.25" , '2018.11.28' AS "2018.11.28" , '2018.11.27' AS "2018.11.27" , '2018.11.24' AS "2018.11.24" , '2018.11.29' AS "2018.11.29"))
ORDER BY planner_id, projects;
Если список значений в столбце ДАТЫ не является постоянным или огромным, то вы должны генерировать этот запрос динамически.Вы должны создать список дат для PIVOT
DECLARE
lv_query CLOB :=
q'[WITH t AS (
SELECT *
FROM (
SELECT 'Planner 2' AS planner_id, '15' AS assigned, '10' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '26%' AS average, '1' AS working_days, '2018.11.26' AS dates FROM dual UNION ALL
SELECT 'Planner 2' AS planner_id, '18' AS assigned, '12' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '21%' AS average, '1' AS working_days, '2018.11.21' AS dates FROM dual UNION ALL
SELECT 'Planner 2' AS planner_id, '16' AS assigned, '13' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '25%' AS average, '1' AS working_days, '2018.11.25' AS dates FROM dual UNION ALL
SELECT 'Planner 2' AS planner_id, '15' AS assigned, '14' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '28%' AS average, '1' AS working_days, '2018.11.28' AS dates FROM dual
UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '1' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '26%' AS average, '1' AS working_days, '2018.11.26' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '28' AS assigned, '2' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '21%' AS average, '1' AS working_days, '2018.11.21' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '26' AS assigned, '3' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '25%' AS average, '1' AS working_days, '2018.11.25' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '4' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '28%' AS average, '1' AS working_days, '2018.11.28' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '5' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '27%' AS average, '1' AS working_days, '2018.11.27' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '26' AS assigned, '6' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '24%' AS average, '1' AS working_days, '2018.11.24' AS dates FROM dual UNION ALL
SELECT 'Planner 1' AS planner_id, '25' AS assigned, '7' AS todo, '0' AS done, '0' AS rejected, '0' AS blocking, '29%' AS average, '1' AS working_days, '2018.11.29' AS dates FROM dual
)
)
SELECT *
FROM (
SELECT planner_id, projects, dates, quantity
FROM t
UNPIVOT (quantity FOR projects IN (assigned AS 'ASSIGNED', todo AS 'TODO', done AS 'DONE', rejected AS 'REJECTED', blocking AS 'BLOCKING', average AS 'AVERAGE', working_days AS 'WORKING_DAYS'))
)
PIVOT (MAX(quantity) FOR (dates) IN (#PivotDates))
ORDER BY planner_id, projects]';
/*
* Generate list of dates for PIVOT
*/
FUNCTION pivotDates
RETURN CLOB
IS
BEGIN
RETURN yourPivotDates;
END pivotDates;
BEGIN
lv_query := REPLACE(lv_query, '#PivotDates', pivotDates());
EXECUTE IMMEDIATE lv_query;
END;
/