Search code examples
google-sheetssumgoogle-sheets-formulagoogle-sheets-querygoogle-query-language

How do I return a result based on criteria versus totals given?


I have a sheet that looks similar to this:

enter image description here

So column A and column B are combined along with a number in column C. What I am trying to do is add up each value in each column (for example: add each C column for each time "Cat" appears, and "Dog" and "Grass", etc) and then find the value in columns A and B that is the highest, and return that value.

So for example, in my example above, Dog would be the formula result because it's C column totals to 28. Is there a formula (or, most likely, a combination of formulas) that can accomplish this?

Also, is it possible to do the inverse: so it would show the least combined value?


Solution

  • for max:

    =INDEX(QUERY({A:A, C:C; B:B, C:C}, 
     "select Col1,sum(Col2) 
      where Col1 is not null 
      group by Col1 
      order by sum(Col2) desc 
      label sum(Col2)''", 0), 1, 1)
    

    for min:

    =INDEX(QUERY({A:A, C:C; B:B, C:C}, 
     "select Col1,sum(Col2) 
      where Col1 is not null 
      group by Col1 
      order by sum(Col2) asc 
      label sum(Col2)''", 0), 1, 1)
    

    0