How to make a SELECT with SUM and DISTINCT?

Asked

Viewed 827 times

0

I need to make a select in the bank using sum and distinct. I tried that:

SELECT DISTINCT `conta_idconta`, SUM(valor) AS ValorTotal FROM contaSaida WHERE conta_empresa_idempresa=1 AND conta_subcategoria_idsubcategoria=4 AND data BETWEEN 2017-03-06 AND 2018-03-06

but returns me NULL

inserir a descrição da imagem aqui

Table content: inserir a descrição da imagem aqui

1 answer

1

Why not use the GROUPBY thus:

SELECT 
    conta_idconta, SUM(valor) AS ValorTotal 
FROM 
    contaSaida 
WHERE 
    conta_empresa_idempresa=1 AND conta_subcategoria_idsubcategoria=4 AND data BETWEEN '2017-03-06' AND '2018-03-06'
GROUP BY
    conta_idconta
  • I tried to use your command above and returned nothing!

  • I want you to show the data separately and the sum of the respective values.

  • As it is not being returned, there is a problem in the condition of the survey, only seeing the contents of the table to know...

  • You need to know if there is any data that matches that filter (Where). If you select without the DISTINCT and without the SUM returns some value?

  • @Paulor.F.Amorin, follow the chart content print in the description. I just posted. Thanks.

  • I saw now that the dates were not between simple quotes, maybe this link can explain better link

  • Sorry, I didn’t understand what you meant about the simple quotes. I circled your sql code with the simple quotes and it didn’t work. In the link you gave me, in the example, he must have mounted the command in the editor, because when you put a field he puts in simple quotes, normal.

  • @Paulor.F.Amorin, got it, the problem was my query that was with the dates without simple quotes. Thanks.

Show 3 more comments

Browser other questions tagged

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