0
Hello
I’m having another problem with a database search. I want to make a SELECT that looks for information such as Cod.empresa, razaosocial, ano e valores(somados). At first, the search for information without the social reason is correct, but when I added the table that contains the social reason, it was a problem, because repeated information is coming, excluding the social reason.
Is there any way around this? because company codes do not agree 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 code of companies and values are equal but the social reason is not.
Thanks for your help!
Follows the structure of the tables:
[Table] information centers
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)
)
I don’t know if you need to tell me, but I think your bank is kind of mocked.. Is the same code with different names correct? if changing
i.empresa
forj.empresa
alters something??– rLinhares
Unfortunately yes, because here in the company the database is messy, because there are numerous tables I mean, I had to do a search in the tables so I could reap the necessary result.
– Andre
and how will you define which name to use? the first (by id)? the smallest (Qtd of characters)?
– rLinhares
I’m sorry I didn’t quite understand the question...
– Andre
supposing that
010011
has 3 different names (type "abc", "abc 2" and "abc 3"), what will be the criteria to choose op1, op2 or op3? would be, for example, the first registered in the base??– rLinhares
Yes, registered by the first of the base.
– Andre
How about replacing the third line with
min(j.razaosocial) as razaosocial,
and remove thej.razaosocial
from GROUP BY there at the end? This takes the first social reason that appears and good, noting that it may not necessarily be the first social reason that was registered in the base for that company.– nunks
Sorry for the delay, I had other tasks and could not focus on this query. First of all, I appreciate the help. Well, 'nunks', I did as you said, but he reduced the information to 172 Rows (correct), but the social reason was the same for all companies.
– Andre