forked from ndleah/8-Week-SQL-Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-sql.sql
96 lines (80 loc) · 2.76 KB
/
query-sql.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
/**************************
CASE STUDY #4 - Data Bank
***************************/
/**************************
A. Customer Nodes Exploration
**************************/
--1. How many unique nodes are there on the Data Bank system?
SELECT COUNT(DISTINCT node_id) AS node_counts
FROM data_bank.customer_nodes;
--2. What is the number of nodes per region?
SELECT
regions.region_name,
COUNT(DISTINCT customer_nodes.node_id) AS node_counts
FROM data_bank.regions
INNER JOIN data_bank.customer_nodes
ON regions.region_id = customer_nodes.region_id
GROUP BY regions.region_name;
--3. How many customers are allocated to each region?
SELECT
regions.region_name,
COUNT(DISTINCT customer_nodes.customer_id) AS customer_counts
FROM data_bank.regions
INNER JOIN data_bank.customer_nodes
ON regions.region_id = customer_nodes.region_id
GROUP BY regions.region_name;
--4. How many days on average are customers reallocated to a different node?
--5. What is the median, 80th and 95th percentile for this same reallocation days metric for each region?
/**************************
B. Customer Transactions
**************************/
/**************************
B. Customer Transactions
**************************/
--1. What is the unique count and total amount for each transaction type?
SELECT
txn_type,
COUNT(txn_type) AS unique_count,
SUM(txn_amount) AS total_amount
FROM data_bank.customer_transactions
GROUP BY txn_type;
--2. What is the average total historical deposit counts and amounts for all customers?
WITH cte_deposit AS (
SELECT
customer_id,
COUNT(txn_type) AS deposit_count,
SUM(txn_amount) AS deposit_amount
FROM data_bank.customer_transactions
WHERE txn_type = 'deposit'
GROUP BY customer_id
)
SELECT
AVG(deposit_count) AS avg_deposit_count,
AVG(deposit_amount) AS avg_deposit_amount
FROM cte_deposit;
--3. For each month - how many Data Bank customers make more than 1 deposit and either 1 purchase or 1 withdrawal in a single month?
WITH cte_customer AS (
SELECT
EXTRACT(MONTH FROM txn_date) AS month_part,
TO_CHAR(txn_date, 'Month') AS month,
customer_id,
SUM(CASE WHEN txn_type = 'deposit' THEN 1 ELSE 0 END) AS deposit_count,
SUM(CASE WHEN txn_type = 'purchase' THEN 1 ELSE 0 END) AS purchase_count,
SUM(CASE WHEN txn_type = 'withdrawal' THEN 1 ELSE 0 END) AS withdrawal_count
FROM data_bank.customer_transactions
GROUP BY
EXTRACT(MONTH FROM txn_date),
TO_CHAR(txn_date, 'Month'),
customer_id
)
SELECT
month,
COUNT(customer_id) AS customer_count
FROM cte_customer
WHERE deposit_count > 1 AND (purchase_count >= 1 OR withdrawal_count >= 1)
GROUP BY
month_part,
month
ORDER BY month_part;
--4. What is the closing balance for each customer at the end of the month?
--5. What is the percentage of customers who increase their closing balance by more than 5%?