Distinct does not work

Asked

Viewed 712 times

0

People, someone can tell me why this query doesn’t work. In fact it works, but brings the values wrong way (duplicating the department field), as if distinct does not work.

select distinct r.departamento, s.status, 
count(s.status) as quantidade from sac as s 
inner join responsaveis as r on s.id_sac = 
r.sac_id where r.departamento = 1 and s.status_sac = 1

inserir a descrição da imagem aqui

  • Is the data return of your query the one from the image? But still your query is not right, apparently, in your case you should use a group by rather than distinct because of that Count.

  • Note the image that the department duplicates, and sac_id also. I need distinct to work in this table and not in the table "Sac"

  • Explain better the data you want to bring and put the structure of the two tables

1 answer

2

distinct groups lines completely equal, which is not the case according to the results that were shown on the grid you posted.

As you are using a grouping function (the Count) the appropriate would be to use a clause "group by", grouping by the unique records that should be counted, this is the correct way to do this distinct, from what I understood from your query.

If my answer does not meet your need please edit your question and include more information about the database tables and their records so you can assist.

  • I didn’t know that distinct groups totally equal lines. That’s why I can’t.

Browser other questions tagged

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