I am running PostgreSQL. I have a table called foo.Its content are:
city|number
'oo'|12
'ss'|11
'ss'|23
'oo'|11
'pp'|21
If I run a query like select count(city) from foo group by city having number<21 I will get
city|number
'oo'|2
'ss'|2
but I want the result to consider all the possible cases of city like this:
city|number
'oo'|2
'ss'|2
'pp'|0
How should the query look like?
This is could be solved with a left join:
Here is a working example
PS: Your example is wrong because ss should have a value of 1, not 2:
Hope this helps.