-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_queries.sql
More file actions
132 lines (88 loc) · 3.17 KB
/
example_queries.sql
File metadata and controls
132 lines (88 loc) · 3.17 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
122
123
124
125
126
127
128
129
130
131
132
-- Number of accounts.
SELECT COUNT(*)
FROM master.dbo.Account
-- Number of people that have a purchase balance which rounds down to either 0,10,...90.
SELECT FLOOR(A.PurchaseBalance/10)*10,
COUNT(*)
FROM master.dbo.Account AS A
WHERE A.PurchaseBalance < 100
GROUP BY FLOOR(A.PurchaseBalance/10)*10
-- Number of accounts for each customer, and amounts owed.
SELECT C.CustomerId,
C.FirstName,
C.LastName,
COUNT(*) N_Accounts,
SUM(A.PurchaseBalance) AS Total_Borrowed
FROM Master.dbo.Account AS A
INNER JOIN Master.dbo.AccountCustomer AS AC ON A.AccountId = AC.AccountId
INNER JOIN Master.dbo.Customer AS C ON AC.CustomerId = C.CustomerId
GROUP BY C.CustomerId,
C.FirstName,
C.LastName
-- Get portfolio composition by Product name.
SELECT P.PortfolioName,
zP.ProductName,
COUNT(*)
FROM Master.dbo.Account AS A
INNER JOIN Master.dbo.Portfolio AS P ON A.PortfolioId = P.PortfolioId
INNER JOIN Master.dbo.zProductType AS zP ON A.ProductType = zP.ProductType
GROUP BY P.PortfolioName,
zP.ProductName
ORDER BY 1,
2
-- Get Customer's collections.
SELECT C.FirstName,
C.LastName,
A.AccountId,
A.PurchaseBalance,
SUM(COL.TransactionAmount) AS Collections
FROM Master.dbo.Account AS A
INNER JOIN Master.dbo.AccountCustomer AS AC ON A.AccountId = AC.AccountId
INNER JOIN Master.dbo.Customer AS C ON AC.CustomerId = C.CustomerId
INNER JOIN Master.dbo.Collections AS COL ON A.AccountId = COL.AccountId
GROUP BY C.FirstName,
C.LastName,
A.AccountId,
A.PurchaseBalance
-- How many open accounts does each customer have.
SELECT C.FirstName,
C.LastName,
SUM(CASE WHEN T.Status = 1 THEN 1 ELSE 0 END) AS N_Closed,
SUM(CASE WHEN T.Status > 0 AND T.Status < 1 THEN 1 ELSE 0 END) AS N_Partial,
COUNT(*) AS N_Total
FROM (
SELECT C.CustomerId,
A.AccountId,
SUM(COL.TransactionAmount) / A.PurchaseBalance Status
FROM Master.dbo.Account AS A
INNER JOIN Master.dbo.AccountCustomer AS AC ON A.AccountId = AC.AccountId
INNER JOIN Master.dbo.Customer AS C ON AC.CustomerId = C.CustomerId
INNER JOIN Master.dbo.Collections AS COL ON A.AccountId = COL.AccountId
GROUP BY C.CustomerId,
A.AccountId,
A.PurchaseBalance
) AS T
INNER JOIN Master.dbo.Customer AS C ON T.CustomerId = C.CustomerId
GROUP BY C.FirstName,
C.LastName
-- Have any Customers settled all accounts?
SELECT C.FirstName,
C.LastName,
SUM(T.Closed) AS N_Closed,
COUNT(*) AS N_Total
FROM (
SELECT C.CustomerId,
A.AccountId,
CASE WHEN SUM(COL.TransactionAmount) - A.PurchaseBalance >= 0 THEN 1 ELSE 0 END AS Closed
FROM Master.dbo.Account AS A
INNER JOIN Master.dbo.AccountCustomer AS AC ON A.AccountId = AC.AccountId
INNER JOIN Master.dbo.Customer AS C ON AC.CustomerId = C.CustomerId
INNER JOIN Master.dbo.Collections AS COL ON A.AccountId = COL.AccountId
GROUP BY C.CustomerId,
A.AccountId,
A.PurchaseBalance
) AS T
INNER JOIN Master.dbo.Customer C ON T.CustomerId = C.CustomerId
GROUP BY C.FirstName,
C.LastName
HAVING SUM(T.Closed) = COUNT(*)