Динамический запрос PlSQL для поворота данных на временных отметках - PullRequest
0 голосов
/ 03 декабря 2018

Я хочу создать динамический запрос SQL, чтобы увидеть даты в столбцах. Строка «Даты» будет добавляться каждый день, поэтому запрос должен быть динамическим.Я хочу, чтобы некоторые столбцы были в строках, а некоторые - в столбцах, будет работать функция Pivot, но мне нужна помощь для создания этого запроса.

Данные: -

 PLANNER_ID|ASSIGNED|TODO|DONE|REJECTED|BLOCKING_PROJECTS|AVERAGE|WORKING_DAYS|DATES
 Planner 1 |25      |0   |0   |0       |0                |0%     |1           |2018.11.26
 Planner 1 |18      |0   |0   |0       |0                |0%     |1           |2018.11.21
 Planner 1 |26      |0   |0   |0       |0                |0%     |1           |2018.11.25
 Planner 1 |25      |0   |0   |0       |0                |0%     |1           |2018.11.28
 Planner 1 |25      |0   |0   |0       |0                |0%     |1           |2018.11.27
 Planner 1 |26      |0   |0   |0       |0                |0%     |1           |2018.11.24
 Planner 1 |25      |0   |0   |0       |0                |0%     |1           |2018.11.29

Требуется вывод: -

  PLANNER_ID|PROJECTS          |21-NOV-2018|24-NOV-2018|25-NOV-2018|26-NOV-2018|27-NOV-2018|28-NOV-2018|29-NOV-2018
  Planner 1 |ASSIGNED          |0          |0          |0          |0          |0          |0          |0
  Planner 1 |TODO              |0          |0          |0          |0          |0          |0          |0
  Planner 1 |AVERAGE           |0%         |0%         |0%         |0%         |0%         |0%         |0%
  Planner 1 |BLOCKING_PROJECTS |0          |0          |0          |0          |0          |0          |0
  Planner 1 |DONE              |0          |0          |0          |0          |0          |0          |0
  Planner 1 |REJECTED          |0          |0          |0          |0          |0          |0          |0
  Planner 1 |WORKING_DAYS      |1          |1          |1          |1          |1          |1          |1

1 Ответ

0 голосов
/ 03 декабря 2018

Ниже приведен пример запроса.

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;
/
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...