Skip to content

Instantly share code, notes, and snippets.

@fabio-stein
Created November 14, 2020 18:34
Show Gist options
  • Save fabio-stein/38e20cb14ae150104172026cfb8aac19 to your computer and use it in GitHub Desktop.
Save fabio-stein/38e20cb14ae150104172026cfb8aac19 to your computer and use it in GitHub Desktop.
Exercicio 2 SQL
WITH bichos_familia AS(
SELECT m.id_familia, SUM(m.quantidadebichosestimacao) AS bichos FROM Morador m
GROUP BY m.id_familia
)
SELECT c.bairro AS 'Bairro', COALESCE(SUM(b.bichos),0) AS 'Bichos de Estimação' FROM Condominio c
LEFT JOIN Familia f ON c.Id = f.id_condominio
LEFT JOIN bichos_familia b ON b.id_familia = f.id
GROUP BY c.bairro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment