Skip to content
Advertisement

SQL Aggregate Statement to Include Null Values and Values Not in a List

tables (see code included)

The result I am looking for is a table with each city, the number of unique yes votes, and the total yes votes. If there are yes votes in the votes table that don’t belong to a city in the users table, then it needs to return “other city” with the same aggregate stats. The result should look like…

enter image description here

Advertisement

Answer

You need a LEFT join of votes to users and conditional aggregation:

See the demo.
Results:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement