Google Sheets Query Language, return 0 - google-sheets

I've been having troubling getting the query function to return 0 when the queried data isn't found.
Given the following data...
With each sheet being a different player, I need to count the number of games each played and the outcome of those games.
Some players may be in the same game as each other so the gameIDs are given as a unique identifier so those games are only counted once, that part I have working.
What I'm trying to do now is record the number of wins team A has in each room.
You can see my problem in the above screenshot.
Here is the sample sheet that has the above data.
This is the formula used for the right most example
=QUERY({QUERY(UNIQUE(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 <> '' and Col3 contains 'A'")),"select Col1, 1");QUERY(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 = '' and Col1 <> '' and Col3 contains 'A'"),"select Col1, 1")},"select Col1, sum(Col2) where Col1 <> '' group by Col1 label sum(Col2) 'TeamA Wins', Col1 'Room'")
Anyone able to help?

You may add dummy data into the formula:
=QUERY(data;{QUERY(UNIQUE(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 <> '' and Col3 contains 'A'")),"select Col1, 1");QUERY(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 = '' and Col1 <> '' and Col3 contains 'A'"),"select Col1, 1")},"select Col1, sum(Col2) where Col1 <> '' group by Col1 label sum(Col2) 'TeamA Wins', Col1 'Room'")
^^^^
data is a named range, it looks like this:
To make it:
paste the data to separate sheet, as shown in picture above.
select range A1:B6
go to Data → Named range → call it "data"
enter the formula and it should work fine now.
Sample file with working formula

Related

Query multiple columns of data in Google Sheets and count number occurrences

I am trying to query two columns of data in Google Sheets and count how many times the values occur.
I have made a Google Sheets formula which works but only returns data in the first column.
=QUERY(QUERY({D2:D,E2:E}, "select Col1, count(Col1) group by Col1"), "select Col1, Col2 order by Col2 desc, Col1 limit 45", 0)
How can I query and count data from both columns together?
DESIRED RESULT
2.38 / 2
5.38 / 2
...
I have made a Google Sheet showing an example. This can be copied by going to File-Make a Copy
https://docs.google.com/spreadsheets/d/1S5fE43JPVgUhZFVx-rjowkCy8N6p_ziIhIWe4MweiXQ/edit?usp=sharing
If your formula gives the correct output, you may need to change your comma with semicolon. That way all your data will be grabbed as a single column:
=QUERY(QUERY({D2:D;E2:E}, "select Col1, count(Col1) group by Col1"), "select Col1, Col2 order by Col2 desc, Col1 limit 45", 0)
Please try:
=QUERY({D2:D;E2:E},"Select Col1, COUNT(Col1) WHERE Col1 IS NOT NULL GROUP BY Col1 ORDER BY COUNT(Col1) DESC LABEL COUNT(Col1)''")

Query Formula using Google Sheets - count data from multiple columns and return results in a single column

I am trying to calculate a count of how many times a course is listed in different columns and return the count in a list of all the courses found in those columns.
I have a spreadsheet that shows the start of my formula...
query(UNIQUE({C2:C21; F2:F21;I2:I21}),"select C,F,I, count(C), count(F), count(I) where B is not null group by C,F,I")
Clear cells A23, A24 and B23 and then try in A23
=query({C2:C21; F2:F21;I2:I21},"select Col1, count(Col1) where Col1 <> '' group by Col1 label Col1 'Fall Courses', Count(Col1) 'Count'")
and see if that helps?
If it does, you can repeat the same logic for 'winter' and 'spring'.
Alternatively, you can also try this single formula
=query(ArrayFormula(split(transpose(split(textjoin("~", true, regexreplace(C1:K1, "(Winter|Spring|Fall)", "_$1")&"_"&C2:K21), "~")), "_")), "Select Col3, Count(Col3) where Col3 <>'' group by Col3 pivot Col2", 0)

Google Sheets QUERY reference a single cell value as a static column

I have the following example spreadsheet: https://docs.google.com/spreadsheets/d/1uv0Lj5sayraH4FOPE1qZ3coLA9A2incLuX2JYD5y7SQ/edit?usp=sharing
I have a query on Sheet 2 that references sheet1 and sheet2. I would like to have the query return 3 columns, Name, ID, and GroupName. GroupName should reference A1 from the sheet.
I cannot find a way to do this, but it seems that it should be possible.
Something like
=query({Sheet1!A:B; Sheet2!A:B},"Select Col1, Col2, A1 Col3 where Col1 is not null")
Try:
=arrayformula(query({iferror(Sheet1!A2:A/0,Sheet1!A1),Sheet1!A2:B; iferror(Sheet2!A3:A/0,Sheet2!A1),Sheet2!A3:B},"where Col2 is not null label Col1 'GroupName'",1))
You can alter the column order like this (select Col2, Col3, Col1):
=arrayformula(query({iferror(Sheet1!A2:A/0,Sheet1!A1),Sheet1!A2:B; iferror(Sheet2!A3:A/0,Sheet2!A1),Sheet2!A3:B},"select Col2, Col3, Col1 where Col2 is not null label Col1 'GroupName'",1))
I was able to get it to work by swapping the semi-colon for a comma
=query({Sheet1!A:B, Sheet2!A:B},"Select Col1, Col2, Col3 where Col1 is not null")
Col3 should be col A from sheet2

Google sheet, select columns within a named range

I'm trying to select a range of data within a named range on a google sheet but after searching I still can't figure it out how to do it.
In this case, I would like to count the amount of time a club appeared within a gameweek.
Here I can do it for all the gameweek tables I have, there is no problem.
Total
=QUERY({B:C;D:E;F:G;H:I;J:K}; "SELECT Col1, SUM(Col2) WHERE Col1 <> '' AND Col1 <> 'GK' AND Col1 <> 'DEF' AND Col1 <> 'MID' AND Col1 <> 'FW' AND Col1 <> 'EXTRA' AND Col1 <> 'Club' AND Col1 <> 'count' AND Col1 MATCHES '^.{0,5}$' GROUP BY Col1 ORDER BY SUM(Col2) DESC LABEL Col1 'Club', SUM(Col2) 'count'")
But I'd like to do the same for each of the gameweek table. So far I have this
Gameweek 1
=QUERY({(B6:B15):(C6:C15);(D6:D15):(E6:E15);(F6:F15):(G6:G15);(H6:H15):(I6:I15);(J6:J15):(K6:K15)}; "SELECT Col1, SUM(Col2) WHERE Col1 <> '' GROUP BY Col1 ORDER BY SUM(Col2) DESC LABEL Col1 'Club', SUM(Col2) 'count'")
but it is not very usable as I'd have to change each range for every next table.
So I tried to make the "Gameweek 1" table as a named range gw1_clubcount and only select its adequate columns similarly as the total so that I would just have to change that for each gameweek but obviously it is not working but that's what I would like to reach.
Gameweek 1 with named range
=QUERY(gw1_clubcount {B:C;D:E;F:G;H:I;J:K}; "SELECT Col1, SUM(Col2) WHERE Col1 <> '' GROUP BY Col1 ORDER BY SUM(Col2) DESC LABEL Col1 'Club', SUM(Col2) 'count'")
I've found the INDEX function where you can select the desired row and column of the named range.That would give something like this below but that's not how you use it.
=QUERY({INDEX(gw1_clubcount,,1):INDEX(gw1_clubcount,,2);INDEX(gw1_clubcount,,3):INDEX(gw1_clubcount,,4);INDEX(gw1_clubcount,,5):INDEX(gw1_clubcount,,6);INDEX(gw1_clubcount,,7):INDEX(gw1_clubcount,,8);INDEX(gw1_clubcount,,9):INDEX(gw1_clubcount,,10)} ; "SELECT Col1, SUM(Col2) WHERE Col1 <> '' GROUP BY Col1 ORDER BY SUM(Col2) DESC LABEL Col1 'Club', SUM(Col2) 'count'")
I'm not very experienced with sheets and a little help would be very appreciated !
Here I created a solution that requires custom function:
To write a custom function:
Create or open a spreadsheet in Google Sheets.
Select the menu item Tools > Script editor.
Delete any code in the script editor.
For this problem, simply copy and paste the code below (stack function) into your script editor.
At the top, click Save save.
To use custom function:
Click the cell where you want to use the function.
Type an equals sign (=) followed by the function name and any input value — for example, =DOUBLE(A1) — and press Enter.
The cell will momentarily display Loading..., then return the result.
Code:
function stack(arr) {
var flatArr = arr.flat();
var filtered = flatArr.filter(function (el) {return el != "";});
const newArr = [];
while(filtered.length) newArr.push(filtered.splice(0,2));
return(newArr);
}
Named Ranges:
Example usage:
GAMEWEEK1:
=QUERY(stack(FLATTEN(gw1_clubcount)), "SELECT Col1, SUM(Col2) WHERE Col1 <> '' GROUP BY Col1 ORDER BY SUM(Col2) DESC LABEL Col1 'Club', SUM(Col2) 'count'")
GAMEWEEK2:
=QUERY(stack(FLATTEN(gw2_clubcount)), "SELECT Col1, SUM(Col2) WHERE Col1 <> '' GROUP BY Col1 ORDER BY SUM(Col2) DESC LABEL Col1 'Club', SUM(Col2) 'count'")
References:
Custom Function
FLATTEN

How to use ARRAYFORMULA in query where clause?

In cell C1:C of my table I got 6 rows with ticket id's. I like to search different spreadsheets to search for those ticket id's and calculate the total hours spent on that ticket.
I have it working using the following formula:
=QUERY({IMPORTRANGE("SPREADSHEETID";"B3:B")\ARRAYFORMULA(TO_PURE_NUMBER(IMPORTRANGE("SPREADSHEETID";"F3:F")-IMPORTRANGE("SPREADSHEETID";"E3:E")))};"SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''")
In this example, C1 is where the ticket ID can be found.
Now I thought I could just wrap QUERY in a ARRAYFORMULA and use C1:C instead of just C1 but that won't work. Now I could just copy and paste the above formula in every cell but there must be a cleaner way.
ANSWER
I used the following formula to make it work, thanks to Max's answer below.
=QUERY(
{
IMPORTRANGE("SPREADSHEETID";"B3:B")\
ARRAYFORMULA(
TO_PURE_NUMBER(
IMPORTRANGE("SPREADSHEETID";"F3:F") - IMPORTRANGE("SPREADSHEETID";"E3:E")
)
)
};
"
SELECT Col1, SUM(Col2)
WHERE Col1 = '" & JOIN("' OR Col1 = '";FILTER(C:C; C:C <> "")) & "'
GROUP BY Col1
LABEL SUM(Col2) ''
")
Sample formula is:
=QUERY({A:B},"select * where Col1 = '"&JOIN("' or Col1 = '",FILTER(D2:D,D2:D<>""))&"'")
No, one cannot create an array of query strings and use arrayformula(query(...)) to run them all at once.
Alternative: instead of
SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''
use the query
SELECT Col1, SUM(Col2) GROUP BY Col1
elsewhere on the sheet, and then use vlookup to look up the sum for each value of Col1 that you want. vlookup can be used inside of arrayformula like this:
=arrayformula(vlookup(C1:C10, E:F, 2, 0))
looks up each of values in C1..C10 in the column E (exact match required) and returns the corresponding value in column F (2nd column of the searched range).

Resources