-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask10.sql
More file actions
44 lines (39 loc) · 1.05 KB
/
task10.sql
File metadata and controls
44 lines (39 loc) · 1.05 KB
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
use financial16_25;
SELECT
c.client_id,
sum(amount - payments) as client_balance,
count(loan_id) as loans_amount
FROM loan as l
INNER JOIN
account a using (account_id)
INNER JOIN
disp as d using (account_id)
INNER JOIN
client as c using (client_id)
WHERE True
AND l.status IN ('A', 'C')
AND d.type = 'OWNER'
GROUP BY c.client_id
HAVING
SUM(amount - payments) > 1000
AND COUNT(loan_id) > 5
SELECT
c.client_id,
sum(amount - payments) as client_balance,
count(loan_id) as loans_amount
FROM loan as l
INNER JOIN
account a using (account_id)
INNER JOIN
disp as d using (account_id)
INNER JOIN
client as c using (client_id)
WHERE True
AND l.status IN ('A', 'C')
AND d.type = 'OWNER'
-- AND EXTRACT(YEAR FROM c.birth_date) > 1990
GROUP BY c.client_id
HAVING
sum(amount - payments) > 1000
-- and count(loan_id) > 5
ORDER BY loans_amount DESC -- tutaj dodajemy sortowanie malejące po liczbie kredytów