Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH
- ko AS
- (
- SELECT 1 id, 'Север' name FROM dual UNION ALL
- SELECT 2 id, 'Запад' name FROM dual UNION ALL
- SELECT 3 id, 'Восток' name FROM dual UNION ALL
- SELECT 4 id, 'Юг' name FROM dual
- ),
- ac AS
- (
- SELECT 1 ko, TO_DATE('01.01.2013','DD.MM.YYYY') oper_date, 100 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('11.07.2024','DD.MM.YYYY') oper_date, 30 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('01.08.2024','DD.MM.YYYY') oper_date, 230 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('11.08.2024','DD.MM.YYYY') oper_date, 40 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('12.08.2024','DD.MM.YYYY') oper_date, 100 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('13.08.2024','DD.MM.YYYY') oper_date, 40 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('14.08.2024','DD.MM.YYYY') oper_date, 90 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('01.09.2024','DD.MM.YYYY') oper_date, 180 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('02.09.2024','DD.MM.YYYY') oper_date, 90 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('11.09.2024','DD.MM.YYYY') oper_date, 180 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('01.10.2024','DD.MM.YYYY') oper_date, 400 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('11.10.2024','DD.MM.YYYY') oper_date, 100 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('13.10.2024','DD.MM.YYYY') oper_date, 400 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('01.11.2024','DD.MM.YYYY') oper_date, 120 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('11.11.2024','DD.MM.YYYY') oper_date, 310 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('12.11.2024','DD.MM.YYYY') oper_date, 100 amount, 0 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('21.11.2024','DD.MM.YYYY') oper_date, 120 amount, 1 direction FROM dual UNION ALL
- SELECT 1 ko, TO_DATE('22.12.2024','DD.MM.YYYY') oper_date, 120 amount, 1 direction FROM dual
- UNION ALL
- SELECT 2 ko, TO_DATE('01.02.2013','DD.MM.YYYY') oper_date, 2725 amount, 0 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('01.08.2024','DD.MM.YYYY') oper_date, 725 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('11.08.2024','DD.MM.YYYY') oper_date, 40 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('01.09.2024','DD.MM.YYYY') oper_date, 90 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('11.09.2024','DD.MM.YYYY') oper_date, 180 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('01.10.2024','DD.MM.YYYY') oper_date, 100 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('11.10.2024','DD.MM.YYYY') oper_date, 380 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('01.11.2024','DD.MM.YYYY') oper_date, 120 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('11.11.2024','DD.MM.YYYY') oper_date, 480 amount, 1 direction FROM dual UNION ALL
- SELECT 2 ko, TO_DATE('21.11.2024','DD.MM.YYYY') oper_date, 80 amount, 1 direction FROM dual
- UNION ALL
- SELECT 3 ko, TO_DATE('01.07.2024','DD.MM.YYYY') oper_date, 125 amount, 1 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('11.07.2024','DD.MM.YYYY') oper_date, 80 amount, 0 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('01.08.2024','DD.MM.YYYY') oper_date, 100 amount, 1 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('11.08.2024','DD.MM.YYYY') oper_date, 40 amount, 0 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('01.09.2024','DD.MM.YYYY') oper_date, 90 amount, 1 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('11.09.2024','DD.MM.YYYY') oper_date, 180 amount, 0 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('12.09.2024','DD.MM.YYYY') oper_date, 10 amount, 0 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('01.10.2024','DD.MM.YYYY') oper_date, 120 amount, 1 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('01.11.2024','DD.MM.YYYY') oper_date, 80 amount, 1 direction FROM dual UNION ALL
- SELECT 3 ko, TO_DATE('01.12.2024','DD.MM.YYYY') oper_date, 80 amount, 0 direction FROM dual
- UNION ALL
- SELECT 4 ko, TO_DATE('01.07.2024','DD.MM.YYYY') oper_date, 120 amount, 1 direction FROM dual UNION ALL
- SELECT 4 ko, TO_DATE('11.07.2024','DD.MM.YYYY') oper_date, 480 amount, 1 direction FROM dual UNION ALL
- SELECT 4 ko, TO_DATE('01.08.2024','DD.MM.YYYY') oper_date, 90 amount, 0 direction FROM dual UNION ALL
- SELECT 4 ko, TO_DATE('11.08.2024','DD.MM.YYYY') oper_date, 180 amount, 1 direction FROM dual UNION ALL
- SELECT 4 ko, TO_DATE('01.11.2024','DD.MM.YYYY') oper_date, 100 amount, 0 direction FROM dual UNION ALL
- SELECT 4 ko, TO_DATE('21.11.2024','DD.MM.YYYY') oper_date, 80 amount, 1 direction FROM dual
- )
- SELECT
- ko.name AS Контрагент,
- SUM(CASE WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -5), 'MM.YYYY') AND ac.direction = 1 THEN ac.amount
- WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -5), 'MM.YYYY') AND ac.direction = 0 THEN -ac.amount
- ELSE 0 END) AS Июль,
- SUM(CASE WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -4), 'MM.YYYY') AND ac.direction = 1 THEN ac.amount
- WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -4), 'MM.YYYY') AND ac.direction = 0 THEN -ac.amount
- ELSE 0 END) AS Август,
- SUM(CASE WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -3), 'MM.YYYY') AND ac.direction = 1 THEN ac.amount
- WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -3), 'MM.YYYY') AND ac.direction = 0 THEN -ac.amount
- ELSE 0 END) AS Сентябрь,
- SUM(CASE WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -2), 'MM.YYYY') AND ac.direction = 1 THEN ac.amount
- WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -2), 'MM.YYYY') AND ac.direction = 0 THEN -ac.amount
- ELSE 0 END) AS Октябрь,
- SUM(CASE WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -1), 'MM.YYYY') AND ac.direction = 1 THEN ac.amount
- WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(ADD_MONTHS(CURRENT_DATE, -1), 'MM.YYYY') AND ac.direction = 0 THEN -ac.amount
- ELSE 0 END) AS Ноябрь,
- SUM(CASE WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(CURRENT_DATE, 'MM.YYYY') AND ac.direction = 1 THEN ac.amount
- WHEN TO_CHAR(ac.oper_date, 'MM.YYYY') = TO_CHAR(CURRENT_DATE, 'MM.YYYY') AND ac.direction = 0 THEN -ac.amount
- ELSE 0 END) AS Декабрь
- FROM
- ko
- LEFT JOIN ac ON ac.ko = ko.id
- GROUP BY
- ko.name
- ORDER BY
- ko.name;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement