I need to condition to bring in group by only the data that do not have a condition

Asked

Viewed 28 times

-1

I have the following query:

SELECT id_grupo, id_analista 
FROM tb_grupo_analista
GROUP BY id_grupo, id_analista;

there is a third column called tp_controle, where I need to condition the query from above to bring me the group only where in that group there is no tp_controle = 2

  • 1

    I don’t understand what you mean by "bring in group by".. can you explain better? group by is used for grouping, ie when you use some function to join/add/count..

1 answer

0

When you use the command SELECT, can use the condition WHERE to condition what you want to bring. Try so:

select id_grupo,id_analista from tb_grupo_analista where tp_controle != 2 group by id_grupo,id_analista;

EDIT:

to group with the condition, according to the comment here in the answer, you can try this way:

select id_grupo,id_analista from tb_grupo_analista where tp_controle = 2
union
select id_grupo,id_analista from tb_grupo_analista where tp_controle != 2 group by id_grupo,id_analista;
  • But I need to group only those who do not have this tp_control = 2 bag?

  • got it. however, when a selection you ask to group, you have no option to group in different ways the elements in the column you chose. thinking here, I believe that you can make a SELECT first without grouping, with tp_control = 2, and give a UNION with another SELECT, with grouping, where tp_control != 2. I will update the response with the command for you to test

Browser other questions tagged

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