Google Sheets query where other sheet equals - google-sheets

I have a workbook with two sheets. I want to query a sheet called Farming that has rows of numbers associated with different objects which I want to sum over.
I want to use query so that the two different sheets can be sorted and filtered without breaking.
I have got this far:
=query(Farming!A2:Z1000, 'select F+G+H+I+J+K+L where "Farming"!B="B7"', 0)
Where the column B on each sheet is the unique reference number for the objects.
However, I'm getting a syntax error and I'm not sure where to go from here.
Thanks for your help!

It's unclear to me whether you're getting B7 from the Farming page or the sheet the formula is on. If it's the former, the first function works. If it's the latter, the second works. These examples are based on text rather than numbers being the value in cell B7. If B7 is a number you don't have to do the quotes.
=query(Farming!A2:Z1000, "select F, G, H, I, J, K, L where B="&"'"&Farming!B7&"'", 0)
=query(Farming!A2:Z1000, "select F, G, H, I, J, K, L where B="&"'"&B7&"'", 0)
Example with the value in B1.

Try
=query(Farming!A2:Z1000, "select F+G+H+I+J+K+L where B='"&B7&"'", 0)
Remarks
The select statement in query
should be quote (") enclosed rather than single quote/apostrophe (') enclosed.
it could reference fields from the data argument by using column names (A, B, C, ) or aliases like Col1, Col2, etc. when the data isn't a reference.
String values inside the SQL statement should be single quote/apostrophe enclosed

Related

Google Sheets Query - Multiple Select Statement If Condition

Currently, I have a worksheet in Google Sheets that uses the formula:
=query('Form Responses 1'!A2:AC,"Select A,B,D,L,M,N,O,P Where L is not null")
This selects the columns A,B,D,L,M,N,O,P Where L is not null but if L is null, I want to select the columns A,B,D,Y,Z,AA,AB,AC instead.
So far, I've tried both of the following:
=query('Form Responses 1'!A2:AC,"Select A,B,D,L,M,N,O,P Where L is not null" AND "Select A,B,D,Y,Z,AA,AB,AC Where Y is not null")
=query('Form Responses 1'!A2:AC,"Select A,B,D,L,M,N,O,P Where L is not null" OR "Select A,B,D,Y,Z,AA,AB,AC Where Y is not null")
Answer
The following formula should produce the result you desire:
=ARRAYFORMULA(IF(ISBLANK(L2:L),{A2:A,B2:B,D2:D,Y2:Y,Z2:Z,AA2:AA,AB2:AB,AC2:AC},{A2:A,B2:B,D2:D,L2:L,M2:M,N2:N,O2:O,P2:P}))
Explanation
First, the =ISBLANK function is used to check whether the value of column L is empty or not. This creates an array of boolean values which are fed into the =IF function. Depending on the result of =ISBLANK one of two possible sets of columns are selected.
Everything is contained within =ARRAYFORMULA so that it functions properly across many rows.
Functions used:
=ISBLANK
=IF
=ARRAYFORMULA

Joining 2 dynamic ranges using {} operator in Google Sheets

I have 2 tables resulting from the query formulas
formula1
=query(BuchungSystem!A2:AZ,"Select C, F, H, I, J, K, L where Q = "& $B$10)
formula2
=arrayformula({{"MWST ",""}} & QUERY(query(BuchungSystem!A2:AZ,"Select M, N, L where Q = "& $B$10),"SELECT Col1*100, SUM(Col2) GROUP BY Col1, Col3 LABEL SUM(Col2) '' , Col1*100 ''"))
and the result is shown as below
I need to combine 2 ranges since the number of rows in table1 keeps changing and so in table2. Since both are query formulas, they would not expand if the underlying cells have data. To avoid such issues, I am thinking both tables can be joined together with 2 empty lines between tables.
I have tried to join the query result ranges using {formula1;formula2} but it gives me an error since both tables have differing columns. How can I merge the tables one below other?
Mind sharing an example sheet? Here is a quick example of how to include empty columns to make the ranges equal in column size.
={{QUERY(G1:K4,"SELECT G,H,I,J,K")};{"","","","",SUM(QUERY(G1:K4,"SELECT G,H,I,J,K"))}}
If you use 6 columns in one range and only 2 in the next, then you need 4 empty columns in the second {} so their sizes remain the same.

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.

How do I add to my QUERY function to add an additional requirement?

=ARRAYFORMULA({
AVERAGE(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(Data!D$2:E856<>"", "♠"&Data!D$2:E856&"♦"&Data!F$2:G856, )),,999^99)),,999^99), "♠")), "♦"),
"select Col2
where lower(Col1) contains '"&LOWER(A2)&"'
offset "&COUNTIF(Data!D$2:E856, A2)-6))})
I have the above formula that I am using. What this does is the following:
The last 6 times A2 shows up in either Column D or E, it accumulates the corresponding value in column F or G. Those 6 values are then turned into an average, as well.
I am trying to add one condition to this. I want it to only take those last 6 instances where column H and I are also something specific.
So when A2 shows up in column D, I only want to use the row if Column I is the value "X". When A2 shows up in column E, I only want to use the row if Column H is "X".
I am unable to get this implemented into my function myself and desperate for some help. One problem is that the "X" search is in reversed column order (ie. when A2 is in D, trying to search I...and when A2 is in E, trying to search G...which isn't the order those appear in the alphabet). Also just not sure where in formula this conditional even needs to go.
Data! just references the sheet I have my dataset dumped into, obviously.

Using QUERY with IMPORTRANGE

According to the Google Help Section I should be able to reference a column in a QUERY by using the "column identifiers ... the one or two character column letter (A, B, C, ...)" - or in this case, G.
The goal of my query is simply to pull information into a new spreadsheet from columns H, J, and K of a different spreadsheet if G is equal to a certain name; in this case, William.
My query:
=QUERY( Survey!G2:K , "select G, H, J, K where G = 'William'" )
works when I use it to call information from a sheet in the same spreadsheet. The problem arises when I try to use this QUERY with IMPORTRANGE. I have used both of these formulas:
=QUERY(IMPORTRANGE("key","'Survey!G2:K'"),"SELECT G, H, J, K WHERE G='William'")
=QUERY(IMPORTRANGE("key","'Survey!Col7:Col11'"),"SELECT Col7, Col8, Col10, Col11 WHERE Col7='William'")
and both return errors. I have included a link to the error that appears for the first QUERY The second error says the same thing, with Col7 replacing G in the text.
What should I be calling the columns in the QUERY?
Is this error due to a problem in my IMPORTRANGE overall?
Please include a link to your sheet.
The Col notation should only go inside the QUERY, not the IMPORTRANGE.
Try this:
=query(importrange("Survey!G2:K"),"Select * where Col7 = 'William'")
This may not be but shows signs of being lack of authorsation. Try IMPORTRANGE on its own and see whether you need to grant access (once off) to the 'other' spreadsheet.

Resources