Do not add field if column equals Mysql

Asked

Viewed 114 times

2

Opa,

I am adding up 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 Cod column can be repeated, if it is repeated the value of the discount column will also be repeated, that is, do not add the discount if you have another same Cod

Select sum(desconto) From ordem where cod != cod and data='2015-10-29'
  • What language are you using in the application? PHP?

  • Here in the company still using VB6

  • I don’t know VB, but I know how to solve your problem using logic with PHP. Would it help? Could you adapt?

  • Please send, if direct change in sql will be better

1 answer

0

When you put cod != cod you’re comparing the code to itself.

An alternative would be to group by code and take 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
  • Opa, it worked partiialmemnte, it did not add the discounts with the same Cod, it should if it is not equal to add the field the sum, if it is equal, add only a field discount the sum. I’m sorry if I didn’t explain it well

  • select Cod , sum(discount) total ......having Count(Cod) = 1)

Browser other questions tagged

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