mirror of
https://github.com/acedanger/budget-database.git
synced 2025-12-05 22:50:13 -08:00
added function to get transactions for a period of time
This commit is contained in:
43
postgres/functions/get_transactions_for_period.sql
Normal file
43
postgres/functions/get_transactions_for_period.sql
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
drop function if exists public.get_transactions_for_period;
|
||||||
|
|
||||||
|
create or replace function public.get_transactions_for_period(
|
||||||
|
p_account_name text
|
||||||
|
, p_start date
|
||||||
|
, p_end date
|
||||||
|
)
|
||||||
|
returns table (
|
||||||
|
bank_name text
|
||||||
|
, account_type text
|
||||||
|
, account_number text
|
||||||
|
, account_friendly_name text
|
||||||
|
, transaction_date date
|
||||||
|
, transaction_description text
|
||||||
|
, transaction_amount numeric(8,2)
|
||||||
|
, transaction_day_of_week text
|
||||||
|
, running_bal numeric
|
||||||
|
)
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS $$
|
||||||
|
begin
|
||||||
|
|
||||||
|
raise notice 'FUNCTION: get_transactions';
|
||||||
|
-- E'\n' is new line
|
||||||
|
raise notice 'INPUT: % p_start = %; % p_end = %;', E'\n', p_start, E'\n', p_end;
|
||||||
|
|
||||||
|
return query
|
||||||
|
select
|
||||||
|
bl.bank_name, bl.account_type, bl.account_number, bl.friendly_name as account_friendly_name
|
||||||
|
, bl.trx_date as transaction_date, bl.trx_description as transaction_description, bl.trx_amount as transaction_amount
|
||||||
|
, bl.day_of_week as transaction_day_of_week, bl.running_bal
|
||||||
|
from public.runbal bl
|
||||||
|
where
|
||||||
|
lower(bl.friendly_name) = trim(lower(p_account_name))
|
||||||
|
and bl.trx_date between p_start and p_end;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
GRANT EXECUTE ON function public.get_transactions_for_period(text, date, date) TO acedanger;
|
||||||
|
GRANT EXECUTE ON function public.get_transactions_for_period(text, date, date) TO budgetuser;
|
||||||
@@ -1,39 +1,47 @@
|
|||||||
|
|
||||||
select get_account_id('Bank of America', '4581')
|
|
||||||
|
|
||||||
CALL public.import_budget_from_csv();
|
|
||||||
CALL public.update_budget_from_import('bank of america', '4581');
|
|
||||||
select 'Import' as TBL, count(1) as REC_CNT from public.budgetimport
|
select 'Import' as TBL, count(1) as REC_CNT from public.budgetimport
|
||||||
union all
|
union all
|
||||||
select 'Detail' as TBL, count(1) as REC_CNT from public.budgetdetails;
|
select 'Detail' as TBL, count(1) as REC_CNT from public.budgetdetails;
|
||||||
|
|
||||||
select acct_id, count(1)
|
|
||||||
from public.budgetdetails b
|
select acct.friendly_name, count(1) as "Number of Transactions"
|
||||||
group by acct_id
|
from
|
||||||
|
public.budgetdetails trx
|
||||||
|
inner join public.accounts acct using(acct_id)
|
||||||
|
group by acct.friendly_name
|
||||||
|
|
||||||
|
|
||||||
select
|
select
|
||||||
lower(trx_description) trx_description
|
acct.friendly_name
|
||||||
, avg(abs(trx_amount))::numeric(7,2) avg_amt
|
, extract(year from trx.trx_date) trx_year
|
||||||
, count(trx_description) rec_cnt
|
, lower(trx.trx_description) trx_description
|
||||||
, min(abs(trx_amount))::numeric(7,2) min_amt
|
, avg(abs(trx.trx_amount))::numeric(7,2) avg_amt
|
||||||
, max(abs(trx_amount))::numeric(7,2) max_amt
|
, count(trx.trx_description) rec_cnt
|
||||||
|
, min(abs(trx.trx_amount))::numeric(7,2) min_amt
|
||||||
|
, max(abs(trx.trx_amount))::numeric(7,2) max_amt
|
||||||
|
, sum(abs(trx.trx_amount))::numeric(9,2) ttl_amt
|
||||||
from
|
from
|
||||||
public.budgetdetails b
|
public.budgetdetails trx
|
||||||
|
inner join public.accounts acct using(acct_id)
|
||||||
where
|
where
|
||||||
trx_date <= current_date
|
trx.trx_date <= current_date
|
||||||
and abs(trx_amount) > 0
|
and abs(trx.trx_amount) > 0
|
||||||
|
-- and lower(acct.friendly_name) like '%savings'
|
||||||
group by
|
group by
|
||||||
lower(trx_description)
|
trx_year, acct.friendly_name, lower(trx_description)
|
||||||
having
|
having
|
||||||
count(1) > 2
|
count(1) > 2
|
||||||
order by
|
order by
|
||||||
rec_cnt desc
|
trx_year desc
|
||||||
, trx_description;
|
, acct.friendly_name
|
||||||
|
, rec_cnt desc
|
||||||
|
, lower(trx.trx_description);
|
||||||
|
|
||||||
|
|
||||||
select
|
select
|
||||||
bal.trx_date
|
bal.friendly_name
|
||||||
|
, bal.trx_date
|
||||||
, bal.day_of_week
|
, bal.day_of_week
|
||||||
, age(trx_date, current_date) as days_from_today
|
, age(trx_date, current_date) as days_from_today
|
||||||
, bal.trx_description
|
, bal.trx_description
|
||||||
@@ -42,9 +50,15 @@ select
|
|||||||
from
|
from
|
||||||
public.runbal bal
|
public.runbal bal
|
||||||
where
|
where
|
||||||
trx_date between current_date and (
|
lower(account_type) = 'checking'
|
||||||
|
and trx_date between
|
||||||
|
(current_date - interval '1 week')
|
||||||
|
and (
|
||||||
current_date + interval '3 weeks'
|
current_date + interval '3 weeks'
|
||||||
)::date
|
)::date
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
|
select *
|
||||||
|
from public.get_transactions_for_period('joint checking', '3/1/2023'::date, '3/31/2023'::date)
|
||||||
|
|||||||
Reference in New Issue
Block a user