On Conditional Sums
Posted by Cliff on September 05, 2001 3:41 PM
This may have already been covered in this group, but I don't recall seeing it and a re-emphasis won't hurt anyway. Most of what I have seen here uses the Boolean logic AND function, where both conditions must be TRUE to give a TRUE result. This appears in the form {=sum(array1="x")*(array2="y")}, where the "*" operator multiplies (or AND's) the two arrays. Think of TRUE as 1 and FALSE as 0, then 0x0=0, 1x0=0, 0x1=0 , and 1x1 =1
However, there is another Boolean function that can be useful; the OR function, which means that if either condition is TRUE then the ouput is TRUE. For example, 0+0=0, 0+1=1, 1+0=1, and 1+1=1. The following example combines the OR function with the AND function, by choosing two alternatives (OR) from column A that meet the criterion column B is an "h" (AND). In other words, in this example, I want the number of all "ana" that are "h" and all "bal" that are "h". The "+" operator is used here to achieve the OR function.
A B
---------
ana h
bal a
bos a
bos h
ana a
bos a
bos a
bal a
ana h
ana h
{=SUM(((A1:A10="ana")+(A1:A10="bal"))*(B1:B10="h"))} = 3. Use Shift+Control+Enter to make this an array formula.
This technique can be extended to many criteria and the combinations of AND's and OR's can be as complex as you want. Just be sure to keep the order of operations correct. For further information about using Boolean logic in this fashion, do a web-search for "truth table".
Cliff