1+ -- Vendor Profitability
2+ -- Highest Operational costs
3+ SELECT v .id AS vendor_id, v .company_name ,
4+ SUM (r .commission_amount ) AS total_revenue,
5+ SUM (c .cost_amount ) AS total_costs,
6+ (SUM (r .commission_amount ) - SUM (c .cost_amount )) AS net_profit
7+ FROM vendors v
8+ JOIN revenue r ON v .id = r .vendor_id
9+ JOIN costs c ON r .order_id = c .order_id
10+ GROUP BY v .id , v .company_name
11+ HAVING net_profit < 0
12+ ORDER BY net_profit ASC ;
13+
14+ -- highest number of operational issues
15+ SELECT v .id AS vendor_id, v .company_name ,
16+ COUNT (c .id ) AS num_issues
17+ FROM vendors v
18+ JOIN revenue r ON v .id = r .vendor_id
19+ JOIN costs c ON r .order_id = c .order_id
20+ WHERE c .type IN (' delivery_failure' , ' re_attempt' , ' return_fraud' )
21+ GROUP BY v .id , v .company_name
22+ ORDER BY num_issues DESC
23+ LIMIT 10 ;
24+
25+ -- Shopper Engagement
26+ -- second purchase members
27+ SELECT s .id AS shopper_id, s .first_name , s .last_name ,
28+ COUNT (o .id ) AS total_orders,
29+ MAX (o .created_at ) AS last_order_date
30+ FROM shoppers s
31+ JOIN orders o ON s .id = o .shopper_id
32+ WHERE s .is_member = TRUE
33+ GROUP BY s .id
34+ HAVING total_orders > 1
35+ ORDER BY last_order_date DESC ;
36+
37+ -- time spent on the portal
38+ SELECT s .id AS shopper_id,
39+ SUM (t .duration_minutes ) AS total_time_spent
40+ FROM shoppers s
41+ JOIN time_spent t ON s .id = t .shopper_id
42+ WHERE s .is_member = TRUE
43+ GROUP BY s .id
44+ ORDER BY total_time_spent DESC
45+ LIMIT 10 ;
46+
47+ -- Product Performance
48+ -- products with the highest sales
49+ SELECT p .id AS product_id, p .name AS product_name,
50+ SUM (oi .quantity ) AS total_sales
51+ FROM products p
52+ JOIN vendor_products vp ON p .id = vp .product_id
53+ JOIN order_items oi ON vp .id = oi .vendor_product_id
54+ GROUP BY p .id , p .name
55+ ORDER BY total_sales DESC
56+ LIMIT 10 ;
57+
58+ -- products with high profitability
59+ SELECT p .id AS product_id, p .name AS product_name,
60+ SUM (oi .quantity * oi .price ) AS total_revenue,
61+ SUM (c .cost_amount ) AS total_costs,
62+ (SUM (oi .quantity * oi .price ) - SUM (c .cost_amount )) AS net_profit
63+ FROM products p
64+ JOIN vendor_products vp ON p .id = vp .product_id
65+ JOIN order_items oi ON vp .id = oi .vendor_product_id
66+ JOIN costs c ON oi .order_id = c .order_id
67+ GROUP BY p .id , p .name
68+ HAVING net_profit > 0
69+ ORDER BY net_profit DESC
70+ LIMIT 10 ;
0 commit comments