Find Top 5 Values With the Highest Sums - google-sheets

I have two columns. Column A has names and Column B has dollar amounts.
I am looking to find the combined sum of each value in Column A and display the top 5 results.
I have tried multiple query formulas to no avail. I have looked through Stack Overflow but have not found an example that matches my problem.
Screenshot example is included below.

you can also try:
SORTN(BYROW(UNIQUE(A:A),LAMBDA(z,{z,SUMIF(A:A,z,B:B)})),5,,2,0)

Try with this QUERY that sums B grouping by A, orders the sums in descendent order and limits to 5 results
=QUERY(A:B,"Select A,SUM(B) group by A order by SUM(B) desc limit 5",)

Related

How can I separate a column into multiple columns based on values?

I have searched on a lot of pages but I cannot find a solution to my problem except in reverse order. I have simplified what I do, but I have a query that comes looking for information in my data sheet. Here there are 3 columns, the date, the amount and the source.
I would like, with a query function, to be able to make different columns which counts the information of column C based on the values of its cells per month, like this
I'm okay with the start of the formula
=QUERY(A2:C,"select month(A)+1, sum(B), count(C) where A is not null group by month(A)+1")
But as soon as I try a little different things by putting 2 query together in an arrayformula, obviously the row count doesn't match as some minus are 0 for some sources.
Do you have a solution for what I'm trying to do? Thank you in advance :)
Solution:
It's not possible in Google Query Language to have a single query statement that has one result grouped by one column and another result grouped by another.
The first two columns can be like this:
=QUERY(A2:C,"select month(A)+1, sum(B) where A is not null group by month(A)+1 label month(A)+1 'Month', sum(B) 'Amount'")
To create the column labels for the succeeding columns, use in the first row, in my example, I1:
=TRANSPOSE(UNIQUE(C2:C))
Then from cell I2, enter this:
=COUNTIFS(arrayformula(month($A$2:$A)),$G2,$C$2:$C,I$1)
Then drag horizontally and vertically to apply to the entire table.
Results:
try:
=INDEX({
QUERY({MONTH(A2:A), B2:C},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label Col1'month',sum(Col2)'amount'"),
QUERY({MONTH(A2:A), B2:C, C2:C},
"select count(Col3) where Col2 is not null group by Col1 pivot Col4")})

Sheets formula for summing values with same name

I am trying to sum values for individual people who have multiple rows in one column and values (prices) in another. I am trying to have a column for the summed values for each person. Right now I am using =sum for each individual person but am looking for a way to code/use a formula to streamline the process.
use:
=QUERY(A:B; "select A,sum(B) where A is not null group by A label sum(B)''")

Google Sheets Combine a column with duplicates and update total sum in another colum

This might be something fairly simple but struggling to find a way to do it.
In Column B, I have a list of foods required.
In Column C, I have the amount needed.
In Column D, I have g (for grams) ml (for mills) etc.
I would like to combine the duplicates in Column B and update the totals from Column C, with the g or ml in Column D beside it.
The list I have has been created by using an array formula based on dropdowns in another sheet.
I have seen people using UNIQUE formula in 1 column (this works) and then a SUMIF formula in another column and then a JOIN formula in another... I tried this but the SUMIF is always returning 0.
Would someone please be able to advise on how I can do this?
TIA :D
It's hard to be sure exactly what you need without seeing the data. But based on my understanding of solely what you've posted, this QUERY formula should generate a condensed mini-report:
=QUERY({B2:D},"Select Col1, SUM(Col2), Col3 WHERE Col1 Is Not Null GROUP BY Col1, Col3 LABEL SUM(Col2) ''")
In plain English, this means "Arrange the data from the range B2:D in the same order as the raw data, but sum the second column's data according to matches in both the first and third columns. Only return results for the raw data where the first column is not blank. Replace the default 'sum' header on the second column with nothing; I don't need it."
This formula assumes that every ingredient will always be attached to the same measurement (e.g., 'salt' in Col B is always paired with 'mg' in Col D, etc.). If this is not the case, you will wind up with ingredients being listed as many times as there are different measures in Col D.

SUM last N values with criteria

I have simple table that looks like this:
All i need is to SUM points for specific player (John) in his last 3 matches.
I was able to come with this formula:
SUMPRODUCT(LARGE((A2:B="John")*(C2:D);{1;2;3}))
The problem is that instead of what I was looking for, it sums the highest 3 values, that can be anywhere in that range.
Is there some similar formula, that can do only the last 3 matches?
I think a SUMPRODUCT can get you there with some constructed arrays using a COUNTIFS() and ROW() to get the most recent 3.
This formula:
=SUMPRODUCT((COUNTIFS(A:B,G2,ROW(A:B)*{1,1},">="&ROW(A:B)*{1,1})<=3)*(A:B=G2),C:D)
on this sheet I made seems to work.
I thnk I have a formula that gives what you want. It's not pretty, and I'm sure it can be made simpler, but this works:
=query( query(
{ arrayformula( {ROW(A1:A) } ),
query(A1:D,"select A, B, C, D",1)
} , "select * order by Col1 desc",1),
"select Col2, Col3, Col4, Col5
where (Col2 ='John' or Col3 = 'John')
order by Col1 desc limit 3",1)
Basically, it adds the row number as an extra column to the data, so that we can sort the data in reverse order by row number. Then we query the result to find the first three occurences of 'John', in either Col A or Col B.
Here is a sample sheet:
https://docs.google.com/spreadsheets/d/1-mhTb5Cpp3D-1OltlmCfwlmM-vc2OknHxfJAyHD7BjI/edit?usp=sharing
Credit to Erik Tyler for a previous answer on a different question, on how to add the row number to a query.
Edit: Updated the sheet to provide the SUM of John's (or any player's) scores from the last three matches. This can be combined with the previous formula, if you want a single formula to place somewhere. Or will you have a list of all the players, and you'll want their last three scores beside each of their names?
If I can simplify the formula, I'll update it here.
Let me know if you need something more than this, or if this has answered your question.
Approach
I would use the query formula to get the cells that you need so that you can leverage the limit statement.
You should put a column with the indexes so that you can order the cells in descending order and take the first 3.
Given that your table headers are:
+-----------------------------------------------+
| INDEX | NAME 1 | NAME 2 | POINTS 1 | POINTS 2 |
+-----------------------------------------------+
I would use this query to get your desired result:
=SUMPRODUCT(QUERY(A2:E, "Select D * E where B = 'John' or C = 'John'" order by A desc limit 3"))

How do I find the third most frequent value in google sheets

I am trying to find the third most frequent value in a google sheet. I saw how to find the second and first one, but I can't find out how to find the third and up. I need to know how to calculate the third, fourth, fifth, sixth, seventh, and eighth places. I am working on a leaderboard system for the school I work at.
I know this is how to find the second most frequent:
=ArrayFormula(MODE(IF((F1:F85=MODE(F1:F85)),"",F1:F85)))
and this is how to find the first:
=mode(F1:F65)
I need to find all the ones that come after that, though.
I need to find all the ones that come after that, though.
The more scalable solution that avoids nested IFs would be something along the lines of:
=ArrayFormula(QUERY({F1:F85,LEN(F1:F85)},"select Col1, count(Col2) where Col2 > 0 group by Col1 order by count(Col2) desc",0))
This produces a table of each number and their associated frequency, sorted from most frequent to least. You can retrieve the specific value from this table using INDEX:
=INDEX(QUERY({F1:F85,LEN(F1:F85)},"select Col1, count(Col2) where Col2 > 0 group by Col1 order by count(Col2) desc",0),n+1,1)
where n is the position you require (1 = most frequent, 2 = second-most frequent, etc).
Your initial This question was referring to a 2-dimensional range, which will be a bit more complicated, but still doable.
Given B1:B99 as our range, if A1 is most common, A2 is second most common, et cetera:
A1 = ArrayFormula(MODE(B1:B99))
A2 = ArrayFormula(MODE(IF((B1:B99=A1),"",B1:B99)))
A3 = ArrayFormula(MODE(IF((B1:B99=A1),"",(IF((B1:B99=A2),"",B1:B99)))))
A4 = ArrayFormula(MODE(IF((B1:B99=A1),"",(IF((B1:B99=A2),"",(IF((B1:B99=A3),"",B1:B99)))))))
I'm curious if anyone has a better solution though.
I'm pretty sure there is a more elegant solution to this than the other answers give.
Try using this Google Visualization API Query:
=query(A:B,"select A, count(B) group by A order by count(B) desc label count(B) 'Count' ",1)
This should give you 2 columns – the first gives the distinct values in your Column A, the second gives the number of occurrences for each value (sorted in descending order).
You should note that it seems that queries on single columns aren't supported, so I admit that the query is a little messy (although it should work if you just keep the B column empty).

Resources