Hello
I have one more problem regarding a search in the database. I want to make a SELECT that searches for information such as company code, social reason, year and values (summed). At first, the search for information without the company name is correct, but when I added the table that contains the business name gave problem, as it is coming back repeated information, taking out the company name.
Is there a way around this? because corporate codes are not in line with social reasons.
Below is the query:
SELECT
i.empresa,
j.razaosocial,
i.exercicio,
(
CASE
WHEN c.lucrocontabil = 0 THEN
c.lucrosimples
WHEN c.lucrosimples = 0 THEN
c.lucrocontabil
END
) AS lucro
FROM
empresas j,
informessocios i
LEFT JOIN (
SELECT
sum(COALESCE(lucrocontabil, 0)) AS lucrocontabil,
sum(COALESCE(lucrosimples, 0)) AS lucrosimples,
empresa,
exercicio
FROM
informessocios
WHERE
exercicio = 2017
AND (
lucrocontabil > 0
OR lucrosimples > 0
)
GROUP BY
empresa,
exercicio,
lucrocontabil,
lucrosimples
) c ON c.empresa = i.empresa
AND c.exercicio = i.exercicio
WHERE
c.exercicio = 2017
GROUP BY
i.empresa,
i.exercicio,
lucro,
j.razaosocial
ORDER BY
i.empresa
Note that the company code and values are the same but the company name is not.
Thanks in advance for your help!
The structure of the tables follows:
[Table] informessocios
CREATE TABLE public.informessocios(
empresa VarChar(6) With Comp NOT NULL 3 [] True
exercicio Long NOT NULL 3 [] True
lucrocontabil Double NULL 3 [] False
lucrosimples Double NULL 3 [] False
CONSTRAINT pk_informessocios PRIMARY KEY (empresa, exercicio, socio)
)
[Table] companies
CREATE TABLE public.empresas(
empresa VarChar(6) With Comp NOT NULL 3 [] True
razaosocial VarChar(50) With Comp NULL 3 [] False
CONSTRAINT pk_empresas PRIMARY KEY (empresa, exercicio)
)