Skip to content

Commit 94f09df

Browse files
committed
feat: added sql function get_income_expense
1 parent bdc06db commit 94f09df

File tree

4 files changed

+75
-135
lines changed

4 files changed

+75
-135
lines changed

src/main/resources/migrations/banking_full_script.txt

Lines changed: 0 additions & 127 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
DROP DATABASE IF EXISTS banking;
2+
3+
CREATE DATABASE banking;
4+
5+
\c banking;
6+
7+
8+
CREATE TABLE IF NOT EXISTS "account" (
9+
"id" bigserial PRIMARY KEY,
10+
"net_salary" double precision,
11+
"account_number" varchar(255) UNIQUE,
12+
"firstname" varchar(255),
13+
"account_type" text,
14+
"bank" varchar(255),
15+
"birthdate" date,
16+
"updated_at" timestamp DEFAULT now(),
17+
"lastname" varchar(255)
18+
);
19+
20+
CREATE TABLE IF NOT EXISTS "account_balance" (
21+
"id" bigserial PRIMARY KEY,
22+
"checked_at" timestamp DEFAULT now(),
23+
"id_account" bigint NOT NULL REFERENCES "account"(id) ON DELETE CASCADE,
24+
"current_balance" double precision
25+
);
26+
27+
CREATE TABLE IF NOT EXISTS "category" (
28+
"id" bigserial PRIMARY KEY,
29+
"only_on" text NOT NULL,
30+
"comments" text,
31+
"name" varchar(255) NOT NULL UNIQUE,
32+
"id_account" bigint REFERENCES "account"(id) ON DELETE CASCADE
33+
);
34+
35+
36+
CREATE TABLE IF NOT EXISTS "interest" (
37+
"id" bigserial PRIMARY KEY,
38+
"id_account" bigint NOT NULL REFERENCES "account"(id) ON DELETE CASCADE,
39+
"effective_interest" integer,
40+
"first_interest" integer NOT NULL,
41+
"deadline" timestamp
42+
);
43+
44+
CREATE TABLE IF NOT EXISTS "transactiongroup" (
45+
"id" bigserial PRIMARY KEY,
46+
"id_account" bigint NOT NULL REFERENCES "account"(id) ON DELETE CASCADE,
47+
"updated_at" timestamp DEFAULT now(),
48+
"label" varchar(255) NOT NULL
49+
);
50+
51+
CREATE TABLE IF NOT EXISTS "transaction" (
52+
"effect_date" date DEFAULT now(),
53+
"amount" double precision NOT NULL,
54+
"receiver" bigint REFERENCES "account"(id) ON DELETE CASCADE,
55+
"created_at" timestamp DEFAULT now(),
56+
"label" varchar(255),
57+
"type" text,
58+
"id" bigserial PRIMARY KEY,
59+
"reference" varchar(255) UNIQUE,
60+
"id_account" bigint NOT NULL REFERENCES "account"(id) ON DELETE CASCADE,
61+
"planed_at" timestamp,
62+
"category" bigint REFERENCES "category"(id) ON DELETE CASCADE,
63+
"id_group" bigint REFERENCES "transactiongroup"(id) ON DELETE CASCADE,
64+
"status" text
65+
);
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
CREATE OR REPLACE FUNCTION get_income_expense_data(period text DEFAULT 'month')
2-
RETURNS TABLE(date date, income double precision, expense double precision) AS $$
1+
CREATE OR REPLACE FUNCTION get_income_expense_data_custom_period(period text DEFAULT 'month')
2+
RETURNS TABLE(period_start date, income double precision, expense double precision)
3+
AS $$
34
BEGIN
45
IF period = 'month' THEN
56
RETURN QUERY
67
SELECT
7-
date_trunc('month', t.effect_date) AS date,
8+
(t.effect_date - (EXTRACT(DAY FROM t.effect_date) - 1) * INTERVAL '1 day') AS period_start,
89
SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END) AS income,
910
SUM(CASE WHEN t.type = 'expense' THEN t.amount ELSE 0 END) AS expense
1011
FROM
1112
transaction t
1213
WHERE
13-
t.effect_date >= date_trunc('month', current_date) - interval '1 month'
14+
t.effect_date >= current_date - interval '1 month'
1415
GROUP BY
15-
date_trunc('month', t.effect_date)
16+
(t.effect_date - (EXTRACT(DAY FROM t.effect_date) - 1) * INTERVAL '1 day')
1617
ORDER BY
17-
date_trunc('month', t.effect_date);
18+
(t.effect_date - (EXTRACT(DAY FROM t.effect_date) - 1) * INTERVAL '1 day');
1819
ELSIF period = 'day' THEN
1920
RETURN QUERY
2021
SELECT
21-
t.effect_date AS date,
22+
t.effect_date AS period_start,
2223
SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END) AS income,
2324
SUM(CASE WHEN t.type = 'expense' THEN t.amount ELSE 0 END) AS expense
2425
FROM
@@ -31,4 +32,5 @@ BEGIN
3132
t.effect_date;
3233
END IF;
3334
END;
34-
$$ LANGUAGE plpgsql;
35+
$$
36+
LANGUAGE plpgsql;

0 commit comments

Comments
 (0)