moving to postgres

This commit is contained in:
Peter Wood
2023-03-04 18:23:05 -05:00
parent 477bfe3cfb
commit 9348a36809
20 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

View File

@@ -0,0 +1,18 @@
create or replace procedure import_budget_from_csv()
language plpgsql
as $procedure$
begin
truncate table public.budgetimport;
copy
public.budgetimport(dt, amount, description)
from
'/usr/share/budget.csv'
delimiter ','
csv
header;
commit;
end;
$procedure$;
GRANT EXECUTE ON PROCEDURE public.update_budget_from_import() TO acedanger;

View File

@@ -0,0 +1,13 @@
CREATE OR REPLACE PROCEDURE public.update_budget_from_import()
LANGUAGE plpgsql
AS $procedure$
begin
truncate public.budgetdetails;
insert into public.budgetdetails (trxdescription, trxdate, trxamount)
select description, cast(dt as date) as dt_converted, amount
from public.budgetimport;
end
$procedure$;
GRANT EXECUTE ON PROCEDURE public.update_budget_from_import() TO acedanger;

View File

@@ -0,0 +1,19 @@
-- public.budgetdetails definition
-- Drop table
DROP TABLE public.budgetdetails;
CREATE TABLE public.budgetdetails (
trxid uuid NOT NULL DEFAULT uuid_generate_v4(),
trxdescription text NOT NULL,
trxdate date NOT NULL,
trxamount money NOT NULL,
insertdttm timestamp NULL DEFAULT now(),
CONSTRAINT budgetdetails_pkey PRIMARY KEY (trxid)
);
-- Permissions
ALTER TABLE public.budgetdetails OWNER TO acedanger;
GRANT ALL ON TABLE public.budgetdetails TO acedanger;
GRANT ALL ON TABLE public.budgetdetails TO budgetuser;

View File

@@ -0,0 +1,11 @@
DROP TABLE public.budgetimport;
create table public.budgetimport (
dt bpchar(10) NULL,
amount numeric(10, 2) NULL,
description bpchar(200) NULL
);
ALTER TABLE public.budgetimport OWNER TO acedanger;
GRANT ALL ON TABLE public.budgetimport TO acedanger;
GRANT ALL ON TABLE public.budgetimport TO budgetuser;

46
postgres/temp.sql Normal file
View File

@@ -0,0 +1,46 @@
/*
truncate table budgetdetails;
CALL public.import_budget_from_csv();
CALL public.update_budget_from_import();
*/
select
lower(trxdescription) trxdescription
, avg(abs(trxamount)) avg_amt
, min(abs(trxamount)) min_amt
, max(abs(trxamount)) max_amt
, count(trxdescription) rec_cnt
from
public.budgetdetails b
where
trxdate <= current_date
and abs(trxamount) > 0
group by
lower(trxdescription)
having
count(1) > 2
order by
rec_cnt desc
, trxdescription;
select
bal.trxdate
, bal.day_of_week
, age(trxdate, current_date) as days_from_today
, bal.trxdescription
, bal.trxamount
, bal.runningbal
from
public.runbal bal
where
trxdate between current_date and (
current_date + interval '2 weeks'
)::date
;
select * from budgetimport b
where dt like '2023-03%'

18
postgres/views/runbal.sql Normal file
View File

@@ -0,0 +1,18 @@
drop view public.runbal;
create view public.runbal
as
select
trxdate
, trxdescription
, trxamount
, to_char(trxdate, 'day') day_of_week
, sum(trxamount) over(
order by
trxdate
, trxamount desc rows unbounded preceding
) runningbal
from
public.budgetdetails