| 7 | | SUM(ta.balance) AS total_balance |
| 8 | | FROM |
| 9 | | transaction_account ta |
| 10 | | JOIN user u ON ta.user_id = u.user_id |
| 11 | | GROUP BY |
| 12 | | u.user_id |
| 13 | | ) |
| 14 | | SELECT |
| 15 | | u.user_id, |
| 16 | | u.user_name, |
| 17 | | SUM(tb.spent_amount) AS total_transaction_amount, |
| 18 | | tub.total_balance AS user_total_balance |
| 19 | | FROM |
| 20 | | transaction_account ta |
| 21 | | JOIN user u ON ta.user_id = u.user_id |
| 22 | | JOIN transaction_breakdown tb ON ta.transaction_account_id = tb.transaction_account_id |
| 23 | | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| 24 | | JOIN total_user_balance tub ON u.user_id = tub.user_id |
| 25 | | WHERE |
| 26 | | t.date <= CURRENT_DATE -- Само активни трансакции до денешен датум |
| 27 | | GROUP BY |
| 28 | | u.user_id, u.user_name, tub.total_balance |
| 29 | | HAVING |
| 30 | | SUM(tb.spent_amount) > tub.total_balance -- Трансакциите го надминуваат ВКУПНИОТ моментален баланс |
| 31 | | ORDER BY |
| 32 | | u.user_id; |
| | 26 | u.user_name, |
| | 27 | t.transaction_id, |
| | 28 | tb.spent_amount AS transaction_amount, |
| | 29 | tub.total_balance AS user_total_balance |
| | 30 | FROM transaction_account ta |
| | 31 | JOIN "user" u ON ta.user_id = u.user_id |
| | 32 | JOIN transaction_breakdown tb ON ta.transaction_account_id = tb.transaction_account_id |
| | 33 | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| | 34 | JOIN total_user_balance tub ON u.user_id = tub.user_id |
| | 35 | WHERE |
| | 36 | tb.spent_amount > tub.total_balance |
| | 37 | AND tb.spent_amount > 0 |
| | 38 | AND t.date <= CURRENT_DATE; |
| | 39 | END; |
| | 40 | $$; |
| | 42 | |
| | 43 | ==== Релациона алгебра |
| | 44 | - U(user_id, user_name) |
| | 45 | - TA(transaction_account_id, user_id, balance) |
| | 46 | - TB(transaction_id, transaction_account_id, spent_amount) |
| | 47 | - T(transaction_id, date) |
| | 48 | |
| | 49 | JOIN за пресметка на вкупен моментален баланс по корисник: |
| | 50 | - J1 ← TA ⨝,,TA.user_id = U.user_id,, U |
| | 51 | - TBAL ← γ,,user_id; Σ(balance) → total_balance,,(J1) |
| | 52 | |
| | 53 | JOIN на сите табели за трансакции: |
| | 54 | - J2 ← TA ⨝,,TA.user_id = U.user_id,, U |
| | 55 | - J3 ← J2 ⨝,,TA.transaction_account_id = TB.transaction_account_id,, TB |
| | 56 | - J4 ← J3 ⨝,,TB.transaction_id = T.transaction_id,, T |
| | 57 | - J5 ← J4 ⨝,,U.user_id = TBAL.user_id,, TBAL |
| | 58 | |
| | 59 | Филтрирање на трансакции кои го надминуваат ВКУПНИОТ моментален баланс: |
| | 60 | - R ← σ,,spent_amount > total_balance ∧ spent_amount > 0 ∧ date ≤ CURRENT_DATE,,(J5) |
| | 61 | |
| | 62 | Подредување по корисник: |
| | 63 | - R_final ← τ,,user_id,,(R) |
| | 64 | |