Do not add field if column equal Mysql

2

Opa,

I am adding the total of a double column, what I need is that, the query does not add up this column if the Cod column is equal. Explaining better, the column can be repeated, if it repeats the value of the discount column will also repeat itself, that is, do not add the discount if you have another equal cod

Select sum(desconto) From ordem where cod != cod and data='2015-10-29'
    
asked by anonymous 29.10.2015 / 13:22

1 answer

0

When you put cod != cod you are comparing the code with itself.

An alternative would be to group by code and pick only the groups that have an occurrence:

select sum(t.total) 
from (select sum(desconto) total 
      from venda 
      group by cod 
      having count(*) = 1) as t
    
29.10.2015 / 14:13