Doubt about sql query

Asked

Viewed 158 times

4

I have a question. I believe it is nothing complex, but I could not solve.

For example, I have this query:

select * from Paciente where ClinicaID = 3

Your result would be these 3 records:

NomePaciente  HoraAtendimento   Ativo  ClinicaID
Teste2          9:30:00         FALSE   3
Teste3           9:00:00        TRUE    3
Teste4          9:00:00         TRUE    3

I need a query to return the lines to my system only if all the lines are Active = true, so if you have any false lines, will not return me any line.

So: If all lines are true in my query, return all lines. If you have any false ones, you should not return any.

  • Evandro’s answer is simple and as precise as possible, the filter for the active field returns only the ones you want.

  • @Thiagooliveira, simple but wrong. Look at @Josédiz’s reply, it is fully correct and also shows graphically that it is correct. If by chance the clinic has a patient with ativo = 'FALSE', then does not display a line. Otherwise, if all lines are marked with ativo = 'TRUE', then returns all lines

3 answers

5

If all lines are true in my query, return all lines. If you have any false, you should not return any.

There are some forms. Here’s one that uses the EXISTS function.

-- código #1
SELECT P.NomePaciente, P.HoraAtendimento, P.ClinicaID, P.Ativo
  from Paciente as P
  where P.ClinicaID = 3
        and P.Ativo = 'TRUE'
        and not exists (SELECT * 
                          from Paciente as P2
                          where P2.ClinicaID = P.ClinicaID
                                and P2.Ativo = 'FALSE');

Demonstration of the functioning of the code, by Leo Caracciolo

If all lines are true in my query, return all lines. true

If you have any false,

paciente

shall not return any.

inserir a descrição da imagem aqui

  • @Leocaracciolo: Thank you for adding the illustrations with the demonstration of the operation of the proposed code.

  • 1

    Conversation transferred to chat http://chat.stackexchange.com/rooms/69429/discussion-between-jose-diz-and-jefferson-quesado

3

Deriving from the idea of @Josédiz, you could make a junction to the left of the table with itself, putting in the junction clause the same clinic identifier and T2 be with T2.ativo = 'FALSE'. Made this junction, if there is any data of T2 being returned in any of your columns, so I should not print that row. The advantage of this method is that if it appears in a line, then it should appear in ALL the lines of the same clinic. Therefore, only check for the nullity of T2.clinicaId in the WHERE is enough to print all lines or no line.

About performance, I have my doubts. I always think that Queries get in the way (this generalization is bad). Now, about reading, surely @Josédiz is easier to read.

SELECT T1.NomePaciente, T1.HoraAtendimento, T1.ClinicaID, T1.Ativo
  from Paciente as T1 left join
    Paciente as T2 on (T1.clinicaId = T2.clinicaId AND T2.ativo = 'FALSE')
  where T1.ClinicaID = 3
        and T1.Ativo = 'TRUE'
        and T2.clinicaId IS NULL
  • 1

    Jefferson, missing add a condition in the WHERE clause, to exclude patients who have at least one record with Active = FALSE.

  • 1

    @Bruno, the good thing was that I said in the text that I would do the checking and I did not! Thank you for the attentive eye

-1

Just add one more filter:

SELECT * FROM Paciente WHERE ClinicaID = 3 AND Ativo = TRUE
  • 1

    But it does not solve the question, because it will return me the line that has the false value.

  • no, if you do as described there, returned only the true ones.

  • 2

    this will return the ones that have active = true, it says se tiver alguma linha false, não me retornará nenhuma linha.

  • 2

    I had to deny that answer for the reasons given by @Leocaracciolo and Ralfe. You didn’t notice that if you have at least one patient in the clinic who is with ATIVO = 'FALSE', no line must be returned

  • @Jeffersonquesado my congratulations! we can not err in the answer that we are punished. It is a "competition" or a forum of help!?

  • I’m using downvote usage guide for answers as discussed in the goal.

Show 1 more comment

Browser other questions tagged

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