-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssessment_Q1.sql
More file actions
27 lines (26 loc) · 1.08 KB
/
Assessment_Q1.sql
File metadata and controls
27 lines (26 loc) · 1.08 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
-- Question 1: High-Value Customers with Multiple Products
-- Purpose: Identify customers who have both savings and investment plans for cross-selling opportunities
-- Approach: Use CTEs to first aggregate customer plan data, then filter for multiple product types
WITH CustomerPlans AS (
-- First get all funded plans per customer and plan type
SELECT
u.id as user_id,
u.name as customer_name,
u.email,
p.plan_type_id,
COUNT(DISTINCT p.id) as plans_count,
SUM(p.amount) as total_deposits
FROM users_customuser u
JOIN plans_plan p ON u.id = p.owner_id
WHERE p.amount > 0 -- Only consider funded plans
GROUP BY u.id, u.name, u.email, p.plan_type_id
)
SELECT
cp1.customer_name,
cp1.email,
COUNT(DISTINCT cp1.plan_type_id) as product_types,
SUM(cp1.total_deposits) as total_value
FROM CustomerPlans cp1
GROUP BY cp1.user_id, cp1.customer_name, cp1.email
HAVING COUNT(DISTINCT cp1.plan_type_id) >= 2 -- Must have at least 2 different plan types
ORDER BY total_value DESC; -- Sort by highest value customers first