Google Sheets QUERY reference a single cell value as a static column - google-sheets

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

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)''")

Google sheet Sumproduct with importrange of unique values

I have data in two sheets (Source Sheet and Result Sheet). I want to import the unique values based on two columns of the source sheet into result sheet along with the product of two columns based on unique value. For more clarity I had entered test data in the Source Sheet with the expected outcome in the Result Sheet. The links of both the sheets are as below
Source sheet https://docs.google.com/spreadsheets/d/13biwAB46kswhIIiLnO-Nn9B8Vf5AAPghsY9YZrYJ470/edit#gid=0
Result Sheet https://docs.google.com/spreadsheets/d/1p9ejduRsFxxDA7vgnO0Y9Wt8Vc9Kwb-gSlhoW1T-ESo/edit#gid=0
Any help on above will be greatly appreciated.
Use nested QUERY() function with IMPORTRANGE().
=QUERY(QUERY(
IMPORTRANGE("13biwAB46kswhIIiLnO-Nn9B8Vf5AAPghsY9YZrYJ470","Sheet1!A2:D"),
"select Col1, Col2, Col3*Col4
label Col3*Col4 ''"),
"select Col1, Col2, sum(Col3)
group by Col1, Col2
label sum(Col3) ''")
If you use E Column in source sheet then you can use only one query-
=QUERY(
IMPORTRANGE("13biwAB46kswhIIiLnO-Nn9B8Vf5AAPghsY9YZrYJ470","Sheet1!A2:E"),
"select Col1, Col2, sum(Col5)
where Col5 is not null
group by Col1, Col2
label sum(Col5) ''")

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: Count unique values in Column B according to unique values in Column A

I have a big table with repeating dates in Column A and some repeating names in Column B. I need to count how many A there are in each month if we count one A for one date. So if there are two A's for 01.06.2021, so it will be counted as 1. The list of A, B, C contains about 20 names.
try:
=INDEX(REGEXREPLACE(""&QUERY(UNIQUE({A:B\ TEXT(A:A; "mm\×mmmm yyyy")});
"select Col2, count(Col2) where Col2 is not null group by Col2 pivot Col3");
"(^\d+×)"; ))
or if you need numeric numbers:
=INDEX(IFERROR(REGEXREPLACE(QUERY(UNIQUE({A:B\ TEXT(A:A; "mm\×mmmm")});
"select Col2, count(Col2) where Col2 is not null group by Col2 pivot Col3");
"(^\d+×)"; ); QUERY(UNIQUE({A:B\ TEXT(A:A; "mm\×mmmm")});
"select Col2, count(Col2) where Col2 is not null group by Col2 pivot Col3")))

Google Sheets Query Language, return 0

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

Resources