Extracting the top five maximum unique values - google-sheets

I am running a Nerf league at a party and want my spreadsheet to show the top five contestants.
Contestants are allowed unlimited entries and only their top score is counted. Entries are being collected on Google Sheets and The Top 5 published on a kiosk screen.
Here is some sample data:
- **Full Name,Score**
- Test Test,3
- Test2 Test2,1
- Test3 Test3,10
- Test4 Test4,3
- Test5 Test5,42
- Test5 Test5,500
- Test6 Test6,20
Here is the formula I have so far (with thanks to tigeravatar):
=INDEX($A$2:$A$28,MATCH(1,INDEX(($B$2:$B$28=LARGE($B$2:$B$28,ROWS(I$1:I1)))*(COUNTIF(I$1:I1,$A$2:$A$28)=0),),0))
This formula shows all maximum values - if, for example, one person has 5 entries that are higher than everyone else, they will all be counted.
The "top five" must show only the entry with the most points from five different contestants.
What do I need to do to show only the top entry that each contestant has provided?

Seems that the formula offered by #AdamL met the requirements:
=QUERY(A2:B28,"select A, max(B) group by A order by max(B) desc limit 5 label max(B) ''",0)

=QUERY(A2:B28,"select A, max(B) group by A order by max(B) desc limit 5 label max(B) ''",0)
<=
It works without a case.
if you have special character in cells in col A, the output/result of function is not right.
For example,
if value in A1 is HN_123_1/2, the output is not right. The special character here is "_" and "/".
Hope this help.

Related

Sum specific numbers in a column (Google sheets)

I have the following spreadsheet containing information about some courses.
I would like to sum the ECTS column but also group the sums by their type General or Tech. E.g here I would have end up with two cells. One containing the number 5 (Total sum of ECTS for courses with type Tech) and another cell containing the number 27.5 (Total sum of ECTS for courses with type General).
Can this be achieved somehow?
Boris, here is a general formula:
=query(A1:E,"select E,sum(C) where E<>'' group by E",1)
This would return a mini-table of the results.
To get just the two cells, modify the above formula to this
=query(A2:E,"select sum(C) where E<>'' group by E
order by sum(C) label sum(C) '' ",0)
You might need to sort them ("order by") a different column to get them in the order your want - this sorts them by increasing value.
UPDATE:
Further explanation of the formula:
" where E<>'' " is effectively saying where (column) E is not equal to blank. It is important to note that query only works reliably with consistent data - only numbers or only text/strings in each column. It will still run if you have mixed data, but the results can be surprising, and query tends to look at what the majority of the data in a column is, either text or numberic.
So the above test will only work for a text column. If you are looking for numbers, you would not use the single quotes, just the equal sign. Eg. where E <> 0 would find rows with numbers not equal to zero in column E.
order by does sorting of the resuslts by one or more columns, and can specify ascending or descending order.
And label sum(C) '' turns off the column header that the query adds when you include an aggregating function like "sum". Or it can be used to re-label the default heading to something else - label sum(C) 'Calculated Sum'
References:
Query - general usage
Query - detailed reference
Formula:
=query(B19:C23, "select sum(B) group by C", -1 )
Everything in the image should be self descriptive.
=SUMPRODUCT(--(B1:B8="a"), A1:A8)
another way to do it using SUMPRODUCT function
B1:B8 : checks for text "a"
-- : the operator decodes True as 1 and False as 0.
A1:A8 : Values to be taken and added

Using COUNTIF's & Total Sums In Google Sheets To Work Out A Financial Total Sum

I'm looking for a specific Google Sheets formula. I believe it's using COUNTIFS & SUMIF, but I can't work out how to do it, any help would be really appreciated.
So, my "C" column has a number of letter combinations in each cell... These are "FHG" "CS" "MO" and afew others. They are a reference to specific market trading strategies and I'm using this sheet to track every trade I make. So C3 may contain "FHG" C4, "MO" C5, "FHG", C6 "FHG" etc.
In the "I" column, there is a 'profit and loss' column which states how much was generated/lost for that particular row... These are formatted to currency.
At the top of my sheet, I have a summary list of each set of letters and I'm using a COUNTIF statement to work out the total number (instance count) of each particular letter combo and from there, I can work out strike rates, percentages etc.
The thing I can't work out is, 'is there a way to count/sum the relative numbers in the "I" column based on a specific letter combo?' For example, let's say running down today's trades, FHG appears on C3, C6, C8, C12, C16 etc. can I collect and sum the numbers (to create a total) from the corresponding P&L column, I3, I6, I8, I12, I16 etc?
My main aim is to have the following at the top in my "Summary Section":
'Letter Combo/Name Of Trading Technique' - 'Number Of Trades' (Using COUNTIFS down C Column) - 'Total % Win Rate' (Number Of Winners/Number Of Total Trades For Said Strategy - This is done using a separate column of Y or N for winners or losers) - The one that I need help with is - "Total Sum Of Money Generated For Each Particular Letter Combo (Trading Strategy)"...... I've worked out how to do the first 3 columns, but I can't work out how to pick the numbers out from the P&L column (I) based on what kind of trade (letter combo) exists in the corresponding (C) column.
If every trade made the same about of money, this would probably be an easy calculation, but because each return is different, this is proving challenging.
Thanks for any insights :)
try:
=QUERY(A:I; "select C,sum(I) where A is not null group by C label sum(I)''")
or just for SB:
=QUERY(A:I; "select C,sum(I) where C = 'SB' group by C label sum(I)''")
if you want a count too:
=QUERY(A:I; "select C,count(C),sum(I) where A is not null group by C")

Using SEARCH function to count occurrence of multiple values

So I've Two lists in Google sheets. one is a (relatively short) list of names, let's say a rooster of employees. The second list is (rather a long) list of shifts, which notes the employees who were present.
for example:
List A - (rooster):
___________________
Mike
Linda
Carrie
Dave
List B - (Import_shift_data):
____________________________
Mike, John
Dave, Linda, Mike
Carrie
Dave, John
Linda
Mike
Dave, Carrie, John, Mike
My goal is to count the presence of each employee.
Now, here are the tricky parts:
List B updates every day, and each cell contains more than one name.
List A also updates, as some employees join the team and other leave.
Each shift could by a day shift, or a night shift (listed in another column next to List B) and I need to count them separately.
The Day/night column is in a parallel column next to shift column, and has one of two values, "Day" or "Night"
So my notion was to create an array formula, who can expand or shrink based on the number of values in List A. The problems is, I Can't yield and results from using the whole {list A} as the first argument in the SEARCH function.
I've tried the foloowing:
=Arrayformula(IF(INDIRECT("A2"):INDIRECT(CONCATENATE("A",MAX(Arrayformula(IF(isblank($A:$A),"",Row($A:$A)))))) = 0,"",COUNTIFs('Import_shift_data'!$P:$P,INDIRECT("A2"):INDIRECT(CONCATENATE("A",MAX(Arrayformula(IF(isblank($A:$A),"",Row($A:$A)))))),'Import_shift_data'!$M:$M,"Night")))
.
But this formula only works for a shift with a single employee.
I also wrote this one:
=Countifs(Arrayformula(ISNUMBER(SEARCH(A2,'Import_shift_data'!$P:$P))),"true",'Import_shift_data'!$M:$M,"Night")
which works fine, but I need to manually drag it up or down every time List A (The rooster) is updated.
So my end game is to have two arrays, one that counts night shifts for each employee, and one who counts day shifts. those arrays should automatically shrink or expand by the size of the rooster. (List A)
Note: If relevant, I may also note that the names in {List A} may contain more than one word, in case there are two employees with the same first name.
A copy of the spreadsheet:
https://drive.google.com/open?id=1HRDAy9-T_rflFpzanZq0fmHpV0jTZg6Rc4vHyOu-1HI
day shift:
=ARRAYFORMULA(QUERY(TRIM(TRANSPOSE(SPLIT(TEXTJOIN(", ", 1, B2:B), ","))),
"select Col1,count(Col1) group by Col1 label count(Col1)''", 0))
night shift:
=ARRAYFORMULA(QUERY(TRIM(TRANSPOSE(SPLIT(TEXTJOIN(", ", 1, C2:C), ","))),
"select Col1,count(Col1) group by Col1 label count(Col1)''", 0))
I Think I've found the Solution, I've used player0's idea of rearranging the data vector and split non-single shifts into single cells.
so basically it goes:
=Arrayformula(CountiF(Transpose(SPlit(Textjoin(" , ",TRUE,QUERY('Import_shift_data'!A:P, "select P where M = 'Night' ", 1))," , ",False)),INDIRECT("A2"):INDIRECT(CONCATENATE("A",MAX(Arrayformula(IF(isblank($A:$A),"",Row($A:$A))))))))
Thanks player0 !

Google Query Language for sum of result using cell reference in query

Hello and thanks for your help. I'm new to GQL but have good SQL experence and think I may be missing something small.
I have 2 sheets i'm working with
Main sheet
Colum G
InstanceID
i-554532f4693fc6186
i-09554fcda5f2f3262
i-0047551ae514412d5
-
Data Sheet
Colum A Colum B
i-554532f4693fc6186 10.12
i-554532f4693fc6186 12.12
i-554532f4693fc6186 13.12
i-554532f4693fc6186 17.12
i-554532f4693fc6186 30.12
I am trying to write a query that will find all the rows that match the Instance ID in column G against the datasheet Column A and return the AVG of all the matches in column B, the top 5 max, and top 5 min.
I'm finding that I can't point the query to a cell for referencing the instance ID. Is there a way?
I'm using this to try to get the max and it works for 1 but I ned the top 5 or any number.
=sort(query('HeC-Metrics'!A:B,"select max(B) Where A = 'i-044532f4693fc6186'"))
I'm OK needing to do different queries for each of the required results, AVG, min, max. I would also like to reference the cell in the G column so I don't have to manually enter the InstanceID.
Thanks your time.
Stephen
So it's just a case of getting the right syntax to use a cell value as a match in the query
=query(Sheet2!A:B,"select avg(B) where A='"&G2&"' group by A label avg(B) ''",1)
Note that you don't really need the group by if you already have a list of distinct ID's to compare against, but you can't have an aggregate like avg without it.
To get the bottom 5, you can use filter & sortn
=transpose(sortn(filter(Sheet2!B:B,Sheet2!A:A=G2),5))
(I have transposed the result to get it in a row (row 2) instead of a column)
or you could use a query
=transpose(query(Sheet2!A:B,"select B where A='"&G2&"' order by B limit 5 label B '' ",1))
Similarly to get the top 5 you could use
=transpose(sortn(filter(Sheet2!B:B,Sheet2!A:A=G2),5,,1,false))
or
=transpose(query(Sheet2!A:B,"select B where A='"&G2&"' order by B desc limit 5 label B '' ",1))
This begs the question of whether you could get these results (a) without needing a list of distinct values and (b) in a single array formula without copying down.
You could certainly get the distinct ID's and averages straight away from a query. Getting the top or bottom n values from a number of groups is much more difficult. I have attempted it in a previous question, but it requires a long and unwieldy formula.

Google Sheets query to extract the top three instances according to criteria

I have a Google Spreadsheet with two sheets.
In sheet "Source" I have a series of countries, cities and landmarks - these are,respectively, in columns A, B and C.
In sheet "Sheet for Query", there are two columns: (A) Country, which has a list of unique country names; and (B) Top 3 cities by Landmark. In column B, I would like to have a Query which gives me, for each country, the top three cities by number of landmark, i.e., the query just has to count the number of instances each city in each country appears and return, for each country, the names of the three cities that come up the most times
This is a sample sheet that I've created in order to demonstrate what I mean: https://docs.google.com/spreadsheets/d/1IPwtAHjwjV1A03o9URws-AtDKw3h9QS9UTT0P1PeVN0/edit?usp=sharing.
Thank you!
I've given this some thought and to 'just' count the number of instances and return the top 3 in each country is surprisingly difficult.
The grouping is straightforward with a query like this
=query(A:C," select A,B,count(C) where A<>'' group by A,B order by A,count(C) desc label A 'Country',B 'City', Count(C) 'Landmarks'",1)
But I don't know of a way of getting the top 3 for each group without going through 2 further steps
(1) Number the results in each group (various ways of doing it but here is one)
=(E1=E2)*D1+1
where the country names after grouping are in column E.
(2) Filter the result for the number in column D being less than 4
=filter(E:G,D:D<4)
You don't specify what qualifies as top (so assuming those are the first listed - higher up the sheet), and you don't clarify number of landmark where there are no numbers in your sheet, but perhaps:
=textjoin(", ",,query(Source!A:C,"select B where A='"&A2&"' limit 3"))
in B2 of sheet for Query, copied down to suit.

Resources