The table The task is: Count the number of customers who simultaneously:
- have more than 5 payments that exeed 5000 dollars
- and have an average payment value of more than 10,000 dollars
I have done it using window function and subquery:
CREATE TABLE Customers (
client INT,
payment INT);
INSERT INTO Customers(client, payment) VALUES
(1, 1000),
(1, 7000),
(1, 6000),
(1, 50000),
(1, 5500),
(1, 5600),
(2, 1000),
(2, 1000);
select client, count(payment) from
(select *, avg(payment) over(partition by client) as avg_payment from Customers) as t1
where payment > 5000
group by client
having count(payment)>5
But I have to make it without window function and subquery. I've been told it is possible to do it only with the use of CASE function. I'll be happy if someone could help me optimize my query.
avg_payment > 10_000condition?1has exactly 5 payments exceeding 5000$, not more than 5 payments.