[SQL๋ฌธ์ ํ๊ธฐ - Advent of SQL 2025 ๐ ] ๋์๋ณ VIP ๊ณ ๊ฐ ์ฐพ๊ธฐ
Dec 23, 2025
๋ฌธ์
๋ด๊ฐ ์์ฑํ ์ ๋ต
MySQL, PostgreSQL, SQLite
with a as (select city_id, customer_id, sum(total_price)-sum(discount_amount) total_spent
from transactions
where not is_returned
group by city_id, customer_id)
select city_id, customer_id, total_spent
from a
where (city_id, total_spent) in (select city_id, max(total_spent) from a group by city_id)
order by 1;Share article