WITH tmp AS (
SELECT REPORT_DATE rdate FROM ODK_INT_rep_p2 WHERE ODK_INT_rep_p2id IN (
SELECT nvl((SELECT ODK_INT_rep_p2id FROM ODK_INT_rep_p2 WHERE TYPE = 2 AND report_date = (SELECT report_date FROM ODK_INT_rep_p2 WHERE ODK_INT_rep_p2id = (SELECT max(ODK_REP_P2ID) FROM odk_svodka_skc2))),
(SELECT ODK_INT_rep_p2id FROM ODK_INT_rep_p2 WHERE TYPE = 2 AND report_date = (SELECT report_date FROM ODK_INT_rep_p2 WHERE ODK_INT_rep_p2id = (SELECT max(ODK_INT_rep_p2id) AS ODK_INT_rep_p2id FROM ODK_INT_rep_p2 WHERE TYPE = 1 AND trunc(report_date)= trunc(current_timestamp))))) AS id1
FROM
dual)),
WITH RECURSIVE cte_months (day) AS (
...
)
WITH RECURSIVE tmp AS (
SELECT REPORT_DATE rdate FROM ODK_INT_rep_p2 WHERE ODK_INT_rep_p2id IN (
SELECT nvl((SELECT ODK_INT_rep_p2id FROM ODK_INT_rep_p2 WHERE TYPE = 2 AND report_date = (SELECT report_date FROM ODK_INT_rep_p2 WHERE ODK_INT_rep_p2id = (SELECT max(ODK_REP_P2ID) FROM odk_svodka_skc2))),
(SELECT ODK_INT_rep_p2id FROM ODK_INT_rep_p2 WHERE TYPE = 2 AND report_date = (SELECT report_date
FROM ODK_INT_rep_p2 WHERE ODK_INT_rep_p2id = (SELECT max(ODK_INT_rep_p2id) AS ODK_INT_rep_p2id
FROM ODK_INT_rep_p2 WHERE TYPE = 1 AND trunc(report_date)= trunc(current_timestamp))))) AS id1
FROM
dual)),
WITH cte_months (day) AS (
...
)