SELECT OP.R_PERS_ACCOUNT_ID,
SUM(OP.AMOUNT),
trunc(OP.ACTUAL_DATE, 'month') - 1
FROM R_PERS_ACCOUNT PA
JOIN NESTED_TABLE NT ON NT.R_PERS_ACCOUNT_ID = PA.R_PERS_ACCOUNT_ID
join r_pers_acc_operation op on op.r_pers_account_id = pa.r_pers_account_id
WHERE OP.R_PERS_ACCOUNT_ID IN NT.R_PERS_ACCOUNT_ID
AND IS_CHARGE_FINE = 0
AND OP.ACTUAL_DATE <= TO_DATE('31.12.2020', 'DD.MM.YYYY')
AND OP.M_OPERATION_TYPE_ID = 1
GROUP BY OP.R_PERS_ACCOUNT_ID, TRUNC(OP.ACTUAL_DATE, 'month') - 1
HAVING SUM(OP.AMOUNT) < 0
SELECT OP.R_PERS_ACCOUNT_ID,
OP.AMOUNT,
trunc(OP.ACTUAL_DATE, 'month') - 1
FROM R_PERS_ACCOUNT PA
JOIN NESTED_TABLE_2 NT_2 ON NT_2.R_PERS_ACCOUNT_ID = PA.R_PERS_ACCOUNT_ID
join r_pers_acc_operation op on op.r_pers_account_id = pa.r_pers_account_id
WHERE OP.R_PERS_ACCOUNT_ID IN NT_2.R_PERS_ACCOUNT_ID
AND IS_CHARGE_FINE = 0
AND OP.ACTUAL_DATE <= TO_DATE('31.12.2020', 'DD.MM.YYYY')
AND OP.M_OPERATION_TYPE_ID = 2
GROUP BY OP.R_PERS_ACCOUNT_ID, TRUNC(OP.ACTUAL_DATE, 'month') - 1, OP.AMOUNT
HAVING SUM(OP.AMOUNT) >= 0;
NESTED_TABLE_2 AS (SELECT OP.R_PERS_ACCOUNT_ID,
SUM(OP.AMOUNT),
trunc(OP.ACTUAL_DATE, 'month') - 1
FROM R_PERS_ACCOUNT PA
JOIN NESTED_TABLE NT ON NT.R_PERS_ACCOUNT_ID = PA.R_PERS_ACCOUNT_ID
join r_pers_acc_operation op on op.r_pers_account_id = pa.r_pers_account_id
WHERE OP.R_PERS_ACCOUNT_ID IN NT.R_PERS_ACCOUNT_ID
AND IS_CHARGE_FINE = 0
AND OP.ACTUAL_DATE <= TO_DATE('31.12.2020', 'DD.MM.YYYY')
AND OP.M_OPERATION_TYPE_ID = 1
GROUP BY OP.R_PERS_ACCOUNT_ID, TRUNC(OP.ACTUAL_DATE, 'month') - 1
HAVING SUM(OP.AMOUNT) < 0)
SELECT OP.R_PERS_ACCOUNT_ID,
OP.AMOUNT,
trunc(OP.ACTUAL_DATE, 'month') - 1
FROM R_PERS_ACCOUNT PA
JOIN NESTED_TABLE_2 NT_2 ON NT_2.R_PERS_ACCOUNT_ID = PA.R_PERS_ACCOUNT_ID
join r_pers_acc_operation op on op.r_pers_account_id = pa.r_pers_account_id
WHERE OP.R_PERS_ACCOUNT_ID IN NT_2.R_PERS_ACCOUNT_ID
AND IS_CHARGE_FINE = 0
AND OP.ACTUAL_DATE <= TO_DATE('31.12.2020', 'DD.MM.YYYY')
AND OP.M_OPERATION_TYPE_ID = 2
GROUP BY OP.R_PERS_ACCOUNT_ID, TRUNC(OP.ACTUAL_DATE, 'month') - 1, OP.AMOUNT
HAVING SUM(OP.AMOUNT) > 0;
Первый и второй запрос относятся к одной таблице, но имеют разные условияТогда нужно использовать конструкцию:
SELECT…
FROM R_PERS_ACCOUNT PA1
JOIN R_PERS_ACCOUNT PA2
ON PA2.PA.R_PERS_ACCOUNT_ID = PA1.PA.R_PERS_ACCOUNT_ID
…
WHERE OP1.M_OPERATION_TYPE_ID = 1 AND OP2.M_OPERATION_TYPE_ID = 2