Загрузка данных


WITH movements AS (
    SELECT
        t.id AS transaction_id,
        t.operation_id,
        t.debit_account_id,
        t.credit_account_id,
        t.value,
        t.value AS balance_change
    FROM back_office.transaction t
    WHERE t.credit_account_id = 41685275

    UNION ALL

    SELECT
        t.id AS transaction_id,
        t.operation_id,
        t.debit_account_id,
        t.credit_account_id,
        t.value,
        -t.value AS balance_change
    FROM back_office.transaction t
    WHERE t.debit_account_id = 41685275
)
SELECT
    o.id AS operation_id,
    ot.code AS operation_type_code,
    o.ops_assignee_decision_id AS decision_id,
    o.effective_date,
    dat.mnemonics AS debit_account_type,
    cat.mnemonics AS credit_account_type,
    m.value,
    m.balance_change
FROM movements m
JOIN back_office.operation o
    ON o.id = m.operation_id
LEFT JOIN back_office.operation_type ot
    ON ot.id = o.operation_type_id
LEFT JOIN back_office.account da
    ON da.id = m.debit_account_id
LEFT JOIN back_office.account_type dat
    ON dat.id = da.account_type_id
LEFT JOIN back_office.account ca
    ON ca.id = m.credit_account_id
LEFT JOIN back_office.account_type cat
    ON cat.id = ca.account_type_id
WHERE o.approved IS NOT NULL
  AND o.deleted IS NULL
ORDER BY o.effective_date, o.id, m.transaction_id;