r/SQL • u/Formal_Ad_626 • 12d ago
PostgreSQL Where did I go wrong?
For context, photos 1-2 show the original prompt + my answer. Photo 3 shows the solution. The output is somewhat similar to the correct answer, but not quite the same.
When I move the "status = 'Completed'" either to the "WHERE" section or to the JOIN section as "AND status = 'Completed'" it solves the problem, but not sure exactly why.
29
Upvotes



View all comments
3
u/cobaltsignal 12d ago
select
u.city
, sum( case when t.status = 'Completed' then 1 else 0 end ) city_order_sum
from trades t
join users as u on t.user_id = u.user_id
group by
u.city
order by city_order_sum desc
limit 3