QUERY with no expected result, what might be missing?

Asked

Viewed 240 times

1

Complementing with current information, I understood the current result, and what it seems is duplicated information even, but in different tables, but this is not even with me because I already arrived and was like this and I’m still an intern I’m just studying this bank. I’m still trying to better understand the use of JOINS.

inserir a descrição da imagem aqui

===========================================================

I’m trying to make an appointment the following way:

SELECT p.idPessoa AS ID_PESSOA,
       p.nome AS TITULAR,
       d.idDependente AS ID_DEPENDENTE,
       d.nome AS DEPENDENTE,
       c.valorAuxilio AS VALOR_AUXILIO
FROM sca_pessoa p

INNER JOIN sca_dependente d ON p.idPessoa = d.idPessoa
INNER JOIN sispro_contrato c ON p.idPessoa = c.idPessoa

WHERE p.nome LIKE '%ADEMAR MAGA%' AND c.idSituacaoContrato = 3 AND c.valorAuxilio IS NOT NULL;

I would like it to have the following result: NAME HOLDER, NAME DEPENDENT and VALUE OF ASSISTANCE that would be the value of each one (2x persons) as shown in the table below.

The name table sispro_contract has the values as shown below:

SELECT idSituacaoContrato, valorAuxilio, idDependente  FROM sispro_contrato where idPessoa = 1361;

What would be wrong or missing?

If I take the AND c.valorAuxilio <> null the return stays like this:

inserir a descrição da imagem aqui

Follow the structures of the tables:

sca_pessoa

idPessoa                     int(10) unsigned
matricula                    varchar(45)
nome                         varchar(100)
telefoneResidencial          char(13)
telefoneCelular              char(13)
telefoneUnidade              char(13)
email                        varchar(100)
dtAdmissao                   date
dtNascimento                 date
idSituacao                   int(4)
SalarioReferencia            int(10) unsigned
idCargo                      int(10)
idLotacao                    int(11)
cpf                          varchar(45)
codigoLotacao                varchar(45)
funcaoValorInicialVertical   varchar(45)
funcaoValorInicialHorizontal varchar(45)
idFuncao                     int(11) unsigned
sexo                         char(1)
rg                           varchar(15)
nomePai                      varchar(44)
nomeMae                      varchar(44)
gsanguineo                   varchar(3)
rh                           varchar(3)
ativo                        char(1)


sca_dependente

idDependente        int(10) unsigned
idPessoa            int(11) unsigned
nome                varchar(100)
dtNascimento        date
idParentesco        int(11) unsigned
pensionista         tinyint(3) unsigned
universitario       tinyint(3) unsigned
pne                 tinyint(3) unsigned
comprovanteEscolar  tinyint(3)


sispro_contrato

idContrato                   int(10) unsigned
idOperadora                  int(10) unsigned
proposta                     varchar(45)
ans                          varchar(45)
valor                        decimal(10,2)
idPessoa                     int(10) unsigned
idFormaDePagamento           int(10) unsigned
idSituacaoContrato           int(10) unsigned
idOperadorUltimaAtualizacao  varchar(45)
dataContrato                 date
dataEncaminhamento           date
dataDeferimento              date
dataHoraUltimaAlteracao      datetime
dataRequerimento             date
dataSituacaoAtual            date
dataSuspensao                date
dataCancelamento             date
idProcesso                   int(11) unsigned
valorAuxilio                 decimal(10,2)
idDependente                 int(10) unsigned
idOperador                   varchar(45)
idTipoOperadora              int(10) unsigned
valorTotalAuxilio            decimal(10,2)
  • Note that it was not that the above QUERY does not return anything, and what I wanted to return would be only NAME, DEPENDENT and VALUE HELP that would be these last two.

  • I believe you’re doing the inner join in the wrong way.

  • @Bacco would like to ask you just one thing about using JOINS, which I did not understand well and I was in doubt. The use of JOINS will be applied only in the tables that have a relationship, that is, a PK for a FK, or simply between two PK, which does not make sense in fact. So when I use ... ON Taba.idPessoa = Tabb.idPessoa(Which is a FK) it’s like this?

  • @phpricardo If this is the case to remove Accept from my answer, I see no problems, since the problem is not solved, but anyway I think it is good to isolate each part of the problem and clarify the doubt. So far I don’t think I can understand how you want the two lines to disappear, since they are in the database so everything indicates.

  • The ON of Join is to say under what conditions the two columns will appear in the same result. You came to see this link? What is the difference between Ner Join and Uter Join

  • Yes, but I don’t understand this question of mine.

  • @phpricardo if there are two lines per person, it may be a problem in the Join or in the database. Does this Ademar not really have two different contracts for these two people? It would be good for you to raise all these questions. How should it appear? What is the right one, the Cícera of 109 or the 269? Is it to add the values, or is it to take just one of them? If it is only one, which one?

  • @phpricardo one thing that could be really wrong is the ON. Post the structure of the tables, suddenly we are not able to notice something because of it. Maybe the correct ID is not the idPersonal on both sides

  • It’s a bit big @Bacco I can put the table desc right here?

  • Here you can download https://copy.com/UCMoq8uJx24g

  • @phpricardo Whenever you post in code format, when it is long it appears scrollbar. But as link already helps.

  • Even reading the two questions, it’s still unclear what you want. Please edit the question with the expected output.

  • @phpricardo, I think you still don’t have it clear: the network [if] is very different of the traditional forums. Here, "solved" is the answer marked as correct by the author of the question, and the "utility" is for all internet. Once you publish your problem here, it is no longer "yours". So be careful with your company code and license cc by sa that all material posted here automatically wins.

Show 8 more comments

2 answers

6


Test like this:

SELECT p.nome AS NOME,
       d.nome AS DEPENDENTE,
       c.valorAuxilio AS VALOR_AUXILIO
FROM sca_pessoa p

-- Mantive o left join, mas no exemplo dado nao vem ao caso.
LEFT JOIN sca_dependente d ON p.idPessoa = d.idPessoa
LEFT JOIN sispro_contrato c ON p.idPessoa = c.idPessoa

WHERE p.nome LIKE '%ADEMAR MAGA%' AND c.idSituacaoContrato = 3 AND c.valorAuxilio IS NOT NULL;

What’s different about that is this:

  • The comparison is correct IS NOT NULL.
    Always use IS NULL and IS NOT NULL instead of comparison with < > =. Nulls are not "comparable".

  • Fixed the syntax of JOIN and ON

See the difference between JOIN types here:
What is the difference between INNER JOIN and OUTER JOIN?

Anyway, if your database has more than one dependent per person, it will surely return more than one line per name. There just grouping, but in case you should not show the name of the dependent, because it will be more than one:

SELECT p.idPessoa AS ID_PESSOA,
   p.nome AS TITULAR,
   SUM(c.valorAuxilio) AS VALOR_AUXILIO_TOTAL
FROM sca_pessoa p

INNER JOIN sca_dependente d ON p.idPessoa = d.idPessoa
INNER JOIN sispro_contrato c ON p.idPessoa = c.idPessoa

WHERE p.nome LIKE '%ADEMAR MAGA%' AND c.idSituacaoContrato = 3 AND c.valorAuxilio IS NOT NULL
-- agrupar pelo titular:
GROUP BY p.idPessoa
-- OU agrupar pelo titular E dependente (use um dos GROUPs so):
GROUP BY p.idPessoa, d.idDependente
  • No mistake, but no return Bacco.

  • Yes, the columns yes.

  • Bacco added an image up there, but I tried with that way and it has the same result (from the image)

  • In the last print I enclosed it shows 4 results being duplicated in the aid value, but what I want is to show only two. It will be R$269.35 and the other R$109.94.

  • Wow, but there you are completely changing the question. Your JOIN is wrong then. I edited the answer again. You want a LEFT JOIN and not Inner.

  • @phpricardo Included a link for you to better check out the main types of Join. See there which type is what really meets your need.

  • I apologize for the lack of understanding of my problem, because to tell the truth neither I also know well how to explain that I am only a trainee here at the company, I have read some things about JOINS, but it is that I still have some doubts. When I added left Join nothing changed but order, but I still have double values.

  • But then these values are actually linked to the same ID. How would you know which of the two is right? You need to see if the database is correct as well. It’s just that it gets a little complicated to understand without seeing the original data and how it was organized.

  • What I still understand here in this bank that I’m moving is that this sispro_contract table it is kind of related to several others and told me that it is recommended that I use it to collect information from others, you know? So with the person’s ID I pull through it (contract table) dependent information, or something like that. :(

  • Yeah I imagine it’s really hard to know what to do without seeing what’s live. :|

  • I just didn’t get it out of the query because it’s being used here -> AND c.idSituacaoContrato = 3

  • Hmm is, but nothing yet of what I want. I’ll see what I do here yet. The fact that it’s a bit complex perhaps for me to explain even.

  • @phpricardo would recommend you first of all understand what you need to do, then go assembling the query gradually and experimenting one part at a time. If you try to work out all the conditions at the same time, you’ll go crazy in there and you won’t know which part is wrong. Change the fields of select to SELECT *, to see everything, take the AS this and AS that, to see better the name of the fields, and then put the WHERE and the groupings. Once you’ve mastered the situation, keep putting back the missing things.

  • I’m going to see this guy, I even know what I want, but I think lack of knowledge is blocking me even. But I appreciate the help.

  • @phpricardo when you have mastered the essential and run into a bigger complication, come back and ask, that there is always someone to help, but remember that the best way to learn is to understand each part of the problem separately.

Show 10 more comments

2

If one of these tables sca_dependente or sispro_contrato have more than one record, your query will bring as many person records as exist in these auxiliary tables.

In this way, I recommend that you group the result by what matters:

SELECT p.idPessoa AS ID_PESSOA,
   p.nome AS TITULAR,
   d.idDependente AS ID_DEPENDENTE,
   d.nome AS DEPENDENTE,
   SUM(c.valorAuxilio) AS VALOR_AUXILIO
FROM sca_pessoa p

INNER JOIN sca_dependente d ON p.idPessoa = d.idPessoa
INNER JOIN sispro_contrato c ON p.idPessoa = c.idPessoa

WHERE p.nome LIKE '%ADEMAR MAGA%' AND c.idSituacaoContrato = 3 AND c.valorAuxilio IS NOT NULL
GROUP BY p.idPessoa,
   p.nome,
   d.idDependente,
   d.nome

Browser other questions tagged

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