-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank-Q2.sql
74 lines (69 loc) · 2.19 KB
/
Bank-Q2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
DROP TABLE IF EXISTS result_table;
CREATE TABLE IF NOT EXISTS result_table(
deposit_id integer,
src_or_des varchar,
amount bigint
);
CREATE OR REPLACE FUNCTION find_src(voucherid varchar, src integer, des integer, amount_ bigint, trn_date date, trn_time varchar) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE cur trn_src_des;
DECLARE notlast boolean = false;
DECLARE sum integer = 0;
BEGIN
FOR cur IN
SELECT *
FROM trn_src_des
WHERE desdep = src and ((trn_date = trndate and trn_time > trntime) OR (trn_date > trndate))
ORDER BY trn_date, trn_time ASC
LOOP
IF cur.amount <> amount_ AND cur.trndate <> trn_date THEN
sum = sum + cur.amount;
END IF;
IF sum <= amount_ + 0.1 * amount_ THEN
notlast = true;
IF cast(cur.sourcedep as boolean) THEN
PERFORM find_src(cur.voucher_id, cur.sourcedep, cur.desdep, cur.amount, cur.trndate, cur.trntime);
ELSE
INSERT INTO result_table VALUES (cur.sourcedep, 'Source', cur.amount);
END IF;
END IF;
END LOOP;
IF notlast = false THEN
INSERT INTO result_table VALUES (src, 'Source', amount_);
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION find_des(voucherid varchar, src integer, des integer, amount_ bigint, trn_date date, trn_time varchar) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE cur trn_src_des;
DECLARE notlast boolean = false;
DECLARE sum int = 0;
BEGIN
FOR cur IN
SELECT *
FROM trn_src_des
WHERE (sourcedep = des) and ((trn_date = trndate and trn_time < trntime) OR (trn_date < trndate))
ORDER BY trn_date, trn_time ASC
LOOP
IF cur.amount <> amount_ AND cur.trndate <> trn_date THEN
sum = sum + cur.amount;
END IF;
IF sum <= amount_ + 0.1 * amount_ THEN
notlast = true;
IF cast(cur.desdep as boolean) THEN
PERFORM find_des(cur.voucher_id, cur.sourcedep, cur.desdep, cur.amount, cur.trndate, cur.trntime);
ELSE
INSERT INTO result_table VALUES (cur.desdep, 'Destination', cur.amount);
END IF;
END IF;
END LOOP;
IF notlast = false THEN
INSERT INTO result_table VALUES (des, 'Destination', amount_);
END IF;
END;
$$;
SELECT * FROM find_src('1234', 1240, 1250, 100000,'2023-01-01', '113008');
SELECT * FROM find_des('1234', 1240, 1250, 100000,'2023-01-01', '113008');
SELECT * FROM result_table