select
  'Import' as TBL,
  count(1) as REC_CNT
from
  public.budgetimport
union all
select
  'Detail' as TBL,
  count(1) as REC_CNT
from
  public.budgetdetails;

select
  acct.acct_friendly_name,
  count(1) as "Number of Transactions"
from
  public.budgetdetails trx
  inner join public.accounts acct using (acct_id)
group by
  acct.acct_friendly_name
select
  acct.acct_friendly_name,
  extract(
    year
    from
      trx.trx_date
  ) trx_year,
  lower(trx.trx_description) trx_description,
  avg(abs(trx.trx_amount))::numeric(7, 2) avg_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
  public.budgetdetails trx
  inner join public.accounts acct using (acct_id)
where
  trx.trx_date <= current_date
  and abs(trx.trx_amount) > 0
  --	and lower(acct.account_friendly_name) like '%savings'
group by
  trx_year,
  acct.acct_friendly_name,
  lower(trx_description)
having
  count(1) > 2
order by
  trx_year desc,
  acct.acct_friendly_name,
  rec_cnt desc,
  lower(trx.trx_description);

select
  bal.account_friendly_name,
  bal.transaction_date,
  bal.transaction_day_of_week,
  age (transaction_date, current_date) as days_from_today,
  bal.transaction_description,
  bal.transaction_amount,
  bal.running_bal
from
  public.runbal bal
where
  lower(account_friendly_name) = 'joint checking'
  and transaction_date between (current_date - interval '1 week') and (current_date + interval '3 weeks')::date;

select
  *
from
  public.get_transactions_for_period (
    'joint checking',
    '3/1/2023'::date,
    '3/31/2023'::date
  )