Search code examples
google-sheetsgoogle-sheets-formula

Using Google Sheets Query, how do aggregate numbers from various columns for a list of names?


I am attempting to use the Google Sheets query function to grab from another sheet that looks kind of like this:

Spreadsheet

What I wish is for a list of the racer winners and how many times they have won a race. My actual sheet is much wider with repeats of tracks and I am trying to see who has won at which track and how many times.

I have tried something like this:

=query({'SheetName'!1:998}, "Select Col1 where (Col2 = 1) or (Col3 = 1) or (Col4 = 1) or (Col5 = 1)")

Which does give me the list of racers who won, but obviously doesn't tell me how many times they have won.

So I tried: =query({'SheetName'!1:998}, "Select Col1, Count(Col1) where (Col2 = 1) or (Col3 = 1) or (Col4 = 1) or (Col5 = 1) group by Col1")

but that just returns the same list with a 1 next to each name.

Any help would be appreciated thanks!

EDIT: Here is a link to a more realistic demo sheet Sheet Maker sample sheet

Below is an example of what I would like the output to be based on me, using the above sheet, for the winners of Track 1.

Racers: Wins:
Frank 1
Larry 1
Joe 2

Edit 2: I have also made a second tab a where I show the attempts I have tried in the past


Solution

  • Added one approach here for you to test out:

    =let(rep_,A2:A, grp_,B2:AO, header_,B1:AO1, size_,1,
         Σ,reduce(wraprows(,columns(rep_)+1+size_,),sequence(columns(grp_)/size_,1,1,size_),lambda(a,c,{a;{rep_,wrapcols(index(header_,c),rows(rep_),index(header_,c)),choosecols(grp_,sequence(size_,1,c))}})),
         Λ,filter(Σ,index(Σ,,column(grp_)+1)<>""),
         query(Λ,"Select Col1,count(Col3) Where Col2 contains 'Finish' AND Col3=1 group by Col1 pivot Col2"))
    
    • Adjust the ranges in the first line of the formula as needed

    enter image description here