LEFT OUTER JOIN

Asked

Viewed 999 times

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, however, I’m not succeeding with the Postgresql syntax. The command I’m 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;
  • you just want two columns counting from the same table ?

  • What is the error? There is an example of how you want the data to be returned?

  • Hello to better evaluate would be nice to post the error that is occurring. group by across the countryside ene_ine

  • Rovann Linhalis, only two columns... then I do it for the others, that is, I take the codmunic as a base to consult the others.

  • Camilo Santos, this is the error: ERROR: column "ene_ine" does not exist LINE 5: WHERE ene_ine = ’s' HINT: Perhaps you Meant to Reference the column "school census.ener_ine" or the column "a. ener_ine". *********Error ******** ERROR: column "ene_ine" does not exist SQL state: 42703 Hint: Perhaps you Meant to Reference the column "school census.ener_ine" or the column "a. ener_ine". Character: 173

  • Heverton, this is the error: ERROR: column "ene_ine" does not exist LINE 5: WHERE ene_ine = ’s' HINT: Perhaps you Meant to Reference the column "school census.ener_ine" or the column "a. ener_ine". *********Error ******** ERROR: column "ene_ine" does not exist SQL state: 42703 Hint: Perhaps you Meant to Reference the column "school census.ener_ine" or the column "a. ener_ine". Character: 173

Show 1 more comment

1 answer

1

Maybe you don’t need any kind of JOIN to make this comparative count, look at this:

SELECT
    codmunic,
    ene_ine,
    count(loc) as energia_ine
FROM
    tabela
GROUP BY
    codmunic,
    ene_ine
ORDER BY
    codmunic,
    ene_ine;
  • 1

    Lacobus, thanks for the strength, but I did need to use the left Join. I ended up going to Mysql, I couldn’t do it in Postgresql, it didn’t work. But I thank everyone who helped me. I would sincerely like to use Postgresql but when I use some lentis there, I can’t run like when I do in Mysql.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.