mirror of
https://github.com/acedanger/budget-database.git
synced 2025-12-05 22:50:13 -08:00
moving to postgres
This commit is contained in:
1
postgres/create uuid-ossp extension.sql
Normal file
1
postgres/create uuid-ossp extension.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
18
postgres/procedures/import_budget_from_csv.sql
Normal file
18
postgres/procedures/import_budget_from_csv.sql
Normal 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;
|
||||||
13
postgres/procedures/import_budget_from_import.sql
Normal file
13
postgres/procedures/import_budget_from_import.sql
Normal 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;
|
||||||
19
postgres/tables/budgetdetails.sql
Normal file
19
postgres/tables/budgetdetails.sql
Normal 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;
|
||||||
11
postgres/tables/budgetimport.sql
Normal file
11
postgres/tables/budgetimport.sql
Normal 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
46
postgres/temp.sql
Normal 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
18
postgres/views/runbal.sql
Normal 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
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user