mirror of
https://github.com/freedbygrace/SQL.git
synced 2026-07-26 11:28:16 +00:00
Expand to Business Analytics: Add Customer, Sales, and KPI models
Major expansion from fraud detection to comprehensive business analytics: DATABASE CHANGES: - Renamed database from 'fraud_detection' to 'business_analytics' - Renamed user from 'fraud_analyst' to 'data_analyst' - Expanded from 20 to 39 tables across 4 business models NEW MODELS (19 tables): 1. Customer Analytics (5 tables): - customer_segments, customer_lifetime_value, churn_predictions - customer_satisfaction, engagement_metrics 2. Sales & Revenue Analytics (6 tables): - product_catalog, sales_transactions, sales_targets - sales_performance, revenue_forecasts 3. KPI & Metrics (8 tables): - kpi_definitions, daily_metrics, monthly_summaries - trend_analysis, dashboard_snapshots - report_definitions, report_executions, data_quality_checks DATA GENERATION: - Extended generate_data.sh with 6 new steps (now 15 total) - Added CLV calculations for all customers - Added churn predictions based on transaction recency - Added 30K customer satisfaction surveys - Added 1M sales transactions linked to 24 products - Added 90 days of daily KPI metrics - Added 24 months of business summaries SQL EXERCISES (3 new levels): - Level 2: Customer Analytics (10 exercises + 3 challenges) - Level 3: Sales & Revenue Analysis (12 exercises + 3 challenges) - Level 4: KPI Dashboards & Metrics (12 exercises + 3 challenges) DOCUMENTATION: - Updated README.md with business analytics focus - Updated QUICKSTART.md with new data generation steps - Updated SETUP_COMPLETE.md with 39-table architecture - Added DATA_MODELS.md with complete model specifications - Added WHATS_NEW.md with migration guide SEED DATA: - Added 8 customer segments (VIP, High Value, etc.) - Added 24 products across 5 categories - Added 16 KPI definitions across 4 categories - Added 8 standard report definitions All changes maintain idempotency and backward compatibility with existing fraud detection functionality.
This commit is contained in:
+64
-33
@@ -19,7 +19,7 @@ docker-compose up -d
|
||||
docker-compose ps
|
||||
```
|
||||
|
||||
You should see both `fraud_detection_db` and `fraud_detection_ui` running.
|
||||
You should see both `business_analytics_db` and `business_analytics_ui` running.
|
||||
|
||||
---
|
||||
|
||||
@@ -34,9 +34,9 @@ chmod +x scripts/setup-database.sh
|
||||
```
|
||||
|
||||
**What this does:**
|
||||
- Creates all 20+ tables
|
||||
- Creates all 39 tables across 4 business models
|
||||
- Sets up indexes and constraints
|
||||
- Loads reference data (countries, merchant categories, etc.)
|
||||
- Loads reference data (countries, merchant categories, customer segments, products, KPIs, etc.)
|
||||
|
||||
**Expected output:**
|
||||
```
|
||||
@@ -63,6 +63,9 @@ chmod +x data/generate_data.sh
|
||||
- Creates 150,000 accounts
|
||||
- Generates 5,000,000 transactions
|
||||
- Creates fraud patterns and alerts
|
||||
- Generates customer analytics (CLV, churn, satisfaction)
|
||||
- Creates sales data (1M sales records)
|
||||
- Generates KPI metrics (90 days of daily metrics)
|
||||
|
||||
**⏱️ Time estimate:**
|
||||
- Fast machine (SSD, 16GB RAM): ~15 minutes
|
||||
@@ -72,10 +75,16 @@ chmod +x data/generate_data.sh
|
||||
**You can monitor progress:**
|
||||
The script shows progress for each step:
|
||||
```
|
||||
[1/9] Loading geographic reference data...
|
||||
[2/9] Generating Customers...
|
||||
[3/9] Generating Accounts...
|
||||
[1/15] Loading geographic reference data...
|
||||
[2/15] Generating Customers...
|
||||
[3/15] Generating Accounts...
|
||||
...
|
||||
[10/15] Generating Customer Lifetime Value data...
|
||||
[11/15] Generating Churn Predictions...
|
||||
[12/15] Generating Customer Satisfaction data...
|
||||
[13/15] Generating Sales Transactions...
|
||||
[14/15] Generating Daily Metrics...
|
||||
[15/15] Generating Monthly Summaries...
|
||||
```
|
||||
|
||||
---
|
||||
@@ -98,7 +107,7 @@ The script shows progress for each step:
|
||||
#### Option B: Command Line (psql)
|
||||
|
||||
```bash
|
||||
docker exec -it fraud_detection_db psql -U fraud_analyst -d fraud_detection
|
||||
docker exec -it business_analytics_db psql -U data_analyst -d business_analytics
|
||||
```
|
||||
|
||||
**Quick commands:**
|
||||
@@ -122,8 +131,8 @@ SELECT COUNT(*) FROM transactions;
|
||||
```
|
||||
Host: localhost
|
||||
Port: 5432
|
||||
Database: fraud_detection
|
||||
Username: fraud_analyst
|
||||
Database: business_analytics
|
||||
Username: data_analyst
|
||||
Password: SecurePass123!
|
||||
```
|
||||
|
||||
@@ -146,43 +155,65 @@ SELECT COUNT(*) FROM customers;
|
||||
-- How many transactions?
|
||||
SELECT COUNT(*) FROM transactions;
|
||||
|
||||
-- How many fraud alerts?
|
||||
SELECT COUNT(*) FROM alerts WHERE status = 'OPEN';
|
||||
-- How many sales records?
|
||||
SELECT COUNT(*) FROM sales_transactions;
|
||||
|
||||
-- How many KPIs are being tracked?
|
||||
SELECT COUNT(*) FROM kpi_definitions WHERE is_active = TRUE;
|
||||
```
|
||||
|
||||
### 2. Find High-Risk Customers
|
||||
### 2. Customer Analytics: High-Value Customers
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
customer_id,
|
||||
first_name,
|
||||
last_name,
|
||||
email,
|
||||
risk_score
|
||||
FROM customers
|
||||
WHERE risk_score > 80
|
||||
ORDER BY risk_score DESC
|
||||
SELECT
|
||||
c.customer_id,
|
||||
c.first_name,
|
||||
c.last_name,
|
||||
clv.clv_score,
|
||||
cs.segment_name
|
||||
FROM customers c
|
||||
JOIN customer_lifetime_value clv ON c.customer_id = clv.customer_id
|
||||
JOIN customer_segments cs ON clv.segment_id = cs.segment_id
|
||||
WHERE cs.segment_name IN ('VIP', 'High Value')
|
||||
ORDER BY clv.clv_score DESC
|
||||
LIMIT 10;
|
||||
```
|
||||
|
||||
### 3. View Recent Transactions
|
||||
### 3. Sales Analytics: Top Products
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
transaction_id,
|
||||
account_id,
|
||||
amount,
|
||||
transaction_date,
|
||||
is_flagged
|
||||
FROM transactions
|
||||
ORDER BY transaction_date DESC
|
||||
LIMIT 20;
|
||||
SELECT
|
||||
p.product_name,
|
||||
p.product_category,
|
||||
COUNT(st.transaction_id) as sales_count,
|
||||
SUM(st.total_amount) as total_revenue
|
||||
FROM product_catalog p
|
||||
JOIN sales_transactions st ON p.product_id = st.product_id
|
||||
GROUP BY p.product_id, p.product_name, p.product_category
|
||||
ORDER BY total_revenue DESC
|
||||
LIMIT 10;
|
||||
```
|
||||
|
||||
### 4. Find Flagged Transactions
|
||||
### 4. KPI Dashboard: Current Status
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
SELECT
|
||||
kd.kpi_name,
|
||||
kd.kpi_category,
|
||||
dm.metric_value,
|
||||
kd.target_value,
|
||||
dm.status
|
||||
FROM kpi_definitions kd
|
||||
JOIN daily_metrics dm ON kd.kpi_id = dm.kpi_id
|
||||
WHERE dm.metric_date = CURRENT_DATE
|
||||
AND kd.is_active = TRUE
|
||||
ORDER BY kd.kpi_category, kd.kpi_name;
|
||||
```
|
||||
|
||||
### 5. Fraud Detection: Flagged Transactions
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
t.transaction_id,
|
||||
t.amount,
|
||||
t.fraud_score,
|
||||
|
||||
Reference in New Issue
Block a user