-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBanking_system_example.sql
230 lines (190 loc) · 5.66 KB
/
Banking_system_example.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use adventureWorks2017
go
begin tran t1
go
IF OBJECT_ID('dbo.client', 'U') IS NOT NULL
DROP TABLE dbo.client;
IF OBJECT_ID('dbo.balance', 'U') IS NOT NULL
DROP TABLE dbo.balance;
IF OBJECT_ID('dbo.transaction', 'U') IS NOT NULL
DROP TABLE dbo.transaction;
create table dbo.client (
client_id numeric(12),
name varchar(50),
category integer )
create table dbo.balance (
client_id numeric(12),
currency varchar(3),
amount numeric(18,2) )
create table dbo.transaction (
transaction_id numeric(12),
client_id_debit numeric(12),
client_id_credit numeric(12),
date_v datetime,
currency varchar(3),
volume numeric(18,2) )
go
insert into dbo.client
values (1,'John Smith', 1),
(2,'John Smith 2', 1),
(3,'John Smith 3', 2),
(4,'John Smith 4', 2)
insert into dbo.balance
values (1,'USD', 100),
(2,'USD', 200),
(3,'USD', 300),
(4,'USD', 400),
(1,'EUR', -100),
(2,'JPN', 200),
(3,'GBP', 300),
(4,'CZK', 400)
insert into dbo.transaction
values (1,1,2,GETDATE(), 'USD', 100),
(2,1,2,GETDATE(), 'USD', 100),
(3,1,2,GETDATE(), 'USD', 100),
(4,1,2,GETDATE(), 'USD', 100),
(5,1,2,GETDATE()-1, 'USD', 100),
(6,1,2,GETDATE()-1, 'USD', 100),
(7,1,2,GETDATE()-1, 'USD', 100),
(8,1,2,GETDATE()-1, 'USD', 100)
select k.category, su.currency, sum(su.amount) 'sum of balances' from dbo.client k
inner join dbo.balance su on su.client_id = k.client_id
group by su.currency, k.category
select currency,
sum(case when amount < 0 then amount else 0 end) 'negative balance',
sum(case when amount > 0 and amount <= 1000000 then amount else 0 end) 'balances below 1 million',
sum(case when amount > 1000000 then amount else 0 end) 'balances over 1 million'
from dbo.balance
group by currency
order by currency asc
commit tran t1
BEGIN TRAN t2
GO
CREATE OR ALTER PROCEDURE dbo.flow
(
@client_id_debit numeric(12),
@client_id_credit numeric(12),
@currency varchar(3),
@volume numeric(18,2),
@Msg nvarchar(MAX)=null OUTPUT
)
AS
BEGIN TRY
DECLARE @amount1 int
DECLARE @amount2 int
INSERT INTO dbo.transaction
(
transaction_id,
client_id_debit,
client_id_credit,
date_v,
currency,
volume
)
VALUES
(
(SELECT MAX (transaction_id) + 1 FROM dbo.transaction),
@client_id_debit,
@client_id_credit,
GETDATE(),
@currency,
@volume
)
SELECT @Msg AS 'Data successfully inserted.'
UPDATE dbo.balance
SET amount = amount + @volume
WHERE client_id = @client_id_credit and currency = @currency
UPDATE dbo.balance
SET amount = amount - @volume
WHERE client_id = @client_id_debit and currency = @currency
END TRY
BEGIN CATCH
SET @Msg=ERROR_MESSAGE()
END CATCH
GO
exec dbo.flow
@client_id_debit = 2,
@client_id_credit = 3,
@currency = USD,
@volume = 10000
GO
COMMIT TRAN t2;
GO
begin tran t3
GO
CREATE OR ALTER PROCEDURE dbo.sum_to_date
(
@client_id numeric(12),
@date datetime
)
AS
BEGIN
DECLARE @suma numeric(15)
DECLARE @suma2 numeric(15)
DECLARE @suma3 numeric(15)
SET @suma = (SELECT TOP 1 a.OV from
(SELECT SUM(T.volume) as OV, T.client_ID_debit FROM DBO.transaction T
WHERE T.date_v > @date
AND T.client_ID_debit = @client_id
AND T.currency = 'USD'
GROUP BY T.client_ID_debit)
a)
SET @suma2 = (SELECT TOP 1 a.OV from
(SELECT SUM(T.volume) as OV, T.client_ID_credit FROM DBO.transaction T
WHERE T.date_v > @date
AND T.client_ID_credit = @client_id
AND T.currency = 'USD'
GROUP BY T.client_ID_credit)
a)
SET @suma3 = (SELECT TOP 1 B.amount FROM DBO.balance B
WHERE B.client_ID = @client_id
AND B.currency = 'USD'
)
SELECT (@suma-@suma2+@suma3) as 'Balance', @date 'Date', @client_id 'client_id', 'USD' as 'currency'
SET @suma = (SELECT TOP 1 a.OV from
(SELECT SUM(T.volume) as OV, T.client_ID_debit FROM DBO.transaction T
WHERE T.date_v > @date
AND T.client_ID_debit = @client_id
AND T.currency = 'CZK'
GROUP BY T.client_ID_debit)
a)
SET @suma2 = (SELECT TOP 1 a.OV from
(SELECT SUM(T.volume) as OV, T.client_ID_credit FROM DBO.transaction T
WHERE T.date_v > @date
AND T.client_ID_credit = @client_id
AND T.currency = 'CZK'
GROUP BY T.client_ID_credit)
a)
SET @suma3 = (SELECT TOP 1 B.amount FROM DBO.balance B
WHERE B.client_ID = @client_id
AND B.currency = 'CZK'
)
SELECT (@suma-@suma2+@suma3) as 'Balance', @date 'Date', @client_id 'client_id', 'CZK' as 'currency'
SET @suma = (SELECT TOP 1 a.OV from
(SELECT SUM(T.volume) as OV, T.client_ID_debit FROM DBO.transaction T
WHERE T.date_v > @date
AND T.client_ID_debit = @client_id
AND T.currency = 'EUR'
GROUP BY T.client_ID_debit)
a)
SET @suma2 = (SELECT TOP 1 a.OV from
(SELECT SUM(T.volume) as OV, T.client_ID_credit FROM DBO.transaction T
WHERE T.date_v > @date
AND T.client_ID_credit = @client_id
AND T.currency = 'EUR'
GROUP BY T.client_ID_credit)
a)
SET @suma3 = (SELECT TOP 1 B.amount FROM DBO.balance B
WHERE B.client_ID = @client_id
AND B.currency = 'EUR'
)
SELECT (@suma-@suma2+@suma3) as 'Balance', @date 'Date', @client_id 'client_id', 'EUR' as 'currency'
END
GO
exec dbo.sum_to_date
@client_id = 2,
@date = '2019-10-21';
select * from dbo.transaction;
select * from dbo.client;
select * from dbo.balance;
commit tran t3;