Untitled

 avatar
unknown
sql
17 days ago
886 B
6
Indexable
--For each region, find customers who spent EUR300 or more on completed orders in Q1 2024 (January-March). Then, only show regions where at least 2 different customers met that threshold. Return the region, customer name, and their total spend.

with spent300_in_q1 as (
select customer_id,
	sum(amount) as total_spend
    from orders
    where order_date >= '2024-01-01' and order_date < '2024-04-01'
		and status = 'completed'
    group by customer_id
having sum(amount) >= 300
),
eligible_regions as (
select region
  from customers as c
  left join spent300_in_q1 as sp on sp.customer_id = c.customer_id
  where sp.customer_id is not null	  
  group by region
  having count(region) >= 2
)
select 	c.region,
		c.customer_name,
		sp.total_spend
 from spent300_in_q1 as sp
left join customers as c on c.customer_id = sp.customer_id
join eligible_regions reg on reg.region = c.region

Editor is loading...
Leave a Comment