===== Листа на трансакции кои го надминале балансот на една сметка подредени хронолошки {{{#!sql CREATE OR REPLACE FUNCTION get_transactions_exceeding_account_balance( p_account_name TEXT ) RETURNS TABLE ( user_id INT, user_name TEXT, account_name TEXT, transaction_id INT, transaction_name TEXT, transaction_amount NUMERIC, transaction_date TIMESTAMP, calculated_balance NUMERIC ) LANGUAGE plpgsql AS $$ BEGIN RETURN QUERY WITH cumulative_balances AS ( SELECT u.user_id, u.user_name, ta.account_name, t.transaction_id, t.transaction_name, tb.spent_amount AS transaction_amount, t.date AS transaction_date, SUM(tb.earned_amount - tb.spent_amount) OVER (PARTITION BY ta.transaction_account_id ORDER BY t.date) AS calculated_balance FROM transaction_account ta JOIN "user" u ON ta.user_id = u.user_id JOIN transaction_breakdown tb ON ta.transaction_account_id = tb.transaction_account_id JOIN transaction t ON tb.transaction_id = t.transaction_id WHERE ta.account_name = p_account_name ) SELECT * FROM cumulative_balances WHERE transaction_amount > calculated_balance AND transaction_amount > 0 ORDER BY transaction_date DESC; END; $$; }}}