-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol2.sql
More file actions
122 lines (83 loc) · 3.2 KB
/
sol2.sql
File metadata and controls
122 lines (83 loc) · 3.2 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
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
/*
Solutions to Part 2 of the Query Tasks.
*/
-- Q5: Which portfolio are people most likely to pay into?
-- i.e., Number of people who have ever paid / number of people in portfolio.
---------------------------------------------------------------------------------------
SELECT A.PortfolioId,
COUNT(A.AccountId) AS NumAccounts,
AVG(
CASE WHEN S.AccountId IS NOT NULL
THEN 1.0
ELSE 0.0
END
) AS PayRate
FROM Account AS A
-- Accounts where payments have been made.
LEFT JOIN (
SELECT A.AccountId,
A.PortfolioId
FROM Account AS A
INNER JOIN Collections AS C ON A.AccountId = C.AccountId
GROUP BY A.AccountId,
A.PortfolioId
HAVING SUM(C.TransactionAmount)>0
) AS S ON A.AccountId = S.AccountId
GROUP BY A.PortfolioId
ORDER BY A.PortfolioId
---------------------------------------------------------------------------------------
-- Q6: Which product type are people most likely to pay into?
---------------------------------------------------------------------------------------
SELECT A.ProductType,
COUNT(A.AccountId) AS NumAccounts,
AVG(
CASE WHEN S.AccountId IS NOT NULL
THEN 1.0
ELSE 0.0
END
) AS PayRate
FROM Account AS A
-- Accounts where payments have been made.
LEFT JOIN (
SELECT A.AccountId,
A.ProductType
FROM Account AS A
INNER JOIN Collections AS C ON A.AccountId = C.AccountId
GROUP BY A.AccountId,
A.ProductType
HAVING SUM(C.TransactionAmount)>0
) AS S ON A.AccountId = S.AccountId
GROUP BY A.ProductType
ORDER BY A.ProductType
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
-- Q7: What is the pay rate over the last 12 months?
-- That is, the number of customers who have paid in the last 12 months divided by the number of customers who
-- had not cleared their balance in the last 12 months.
---------------------------------------------------------------------------------------
SELECT * --SUM(
--CASE WHEN S.SumPaid12Months IS NOT NULL
--THEN 1.0
--ELSE 0.0
--END) -- AS NumPayed
FROM Account AS A
-- Accounts with payments in the last 12 months.
LEFT JOIN (
SELECT A.AccountId,
SUM(C.TransactionAmount) AS SumPaid12Months
FROM Account AS A
INNER JOIN Collections AS C ON A.AccountId = C.AccountId
WHERE C.TransactionDate > DATEADD(year,-1,GETDATE())
GROUP BY A.AccountId
HAVING SUM(C.TransactionAmount)>0
)
AS S ON A.AccountId = S.AccountId
-- Accounts of customers who had not cleared their balances in last 12 months.
LEFT JOIN (
SELECT A.AccountId
FROM Account AS A
INNER JOIN Collections AS C ON A.AccountId = C.AccountId
GROUP BY A.AccountId, A.PurchaseBalance
HAVING SUM(C.TransactionAmount) < A.PurchaseBalance
) AS T ON A.AccountId = T.AccountId
--ORDER BY A.AccountId