Skip to content
Advertisement

How to SUM multiple fields with condition

Here is my table structure

the thing is, i want to SUM field (col1,col2,col3,col4) but only the value 1. so based on the record above. the result would be 3 (col1+col2+col4), becauser value from col3 is 2 not 1.

sorry for bad grammar.

Advertisement

Answer

Either you mean:

  1. You want to calculate a new column on each row which is the sum of columns 1 through 4 if those columns are 1 (which is the same as the count of columns which are 1), or
  2. You want to calculate the sum of those columns but only when the row values of those columns are 1

For 1:

For 2:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement