LEFT OUTER JOIN

1

I have a table in PostgreSQL. I would like to count two columns where one has more record than the other. If I use LEFT OUTER JOIN I think it will work, though, I'm not getting with the PostgreSQL syntax. The command I am using is as follows:

SELECT codmunic, ene_ine, count(loc) as energia_ine
from tabela a left join
    (SELECT codmunic, COUNT(loc) as energia_ine
      FROM tabela
      WHERE ene_ine = 's'
      GROUP BY codmunic) as ag_caim
      using (codmunic)
      group by codmunic;
    
asked by anonymous 15.06.2018 / 16:48

1 answer

0

Maybe you do not need any kind of JOIN to make this comparative count, see:

SELECT
    codmunic,
    ene_ine,
    count(loc) as energia_ine
FROM
    tabela
GROUP BY
    codmunic,
    ene_ine
ORDER BY
    codmunic,
    ene_ine;
    
18.06.2018 / 03:24