Understanding "select X where Y = ..." in a google sheets QUERY function - google-sheets

I'm trying to figure out how to parse this google sheets function:
=IFERROR(QUERY($A$2:$F$1000, "select F where A="&A4&" "),"")
I'm having trouble understanding the "select F where A="&A4&" part. The function is applied to an entire column. For some of the rows, this function returns a number, for others it returns a blank. The A column which it is referencing is entirely composed of 6-digit numbers.
What is going on such that sometimes the function returns a number and sometimes a blank?
Also, why are the ampersands important? If I take away the ampersands, the function returns an error.

You need to fix the quotes around A4.
=IFERROR(QUERY($A$2:$F$1000, "select F where A='"&A4&"'"),"")

'"&A4&"'
means what is in cell A4
The & means to concatenate.
In this case the literal contents of A4 into the query formula.
Notice that the query has 4 "s. ie ""
"&""
The single quotes are to make the contents of A4 a string.
where A=
so where contents of A2 to A1000 matches the contents of A4.
It would definitely match on A4, (and any other Col A cell that had the same contents.)
in which case it would return F4 because of the
"select F"
means show/return column F in the results

You should try the following:
=arrayformula(if(eq(F2:F,A2:A),F2:F,))
It is hard to suggest the right formula without seeing what you are working with or what the expected result looks like, so if this doesn't work, please share your sample spreadsheet.

Related

Summing specific values in a joined list

I am having some difficulties summing up some values in Google Sheets. In my spreadsheet, from multiple other tabs, values and bonuses are combined into one cell (Cell B1 in this example). The format of each "unit" of data is Name,5%xxx (Where "Name" is the name of the item, "5%" represents the sum I want to add, mostly always a percentage, and "xxx" separates one unit from the next). As you can see in cell B1, there are two instances where "Parkour" receives a bonus to sum up (from different sources).
Parkour,5%xxxParkour (Subskill: Sense of Balance),10%xxxParkour,2%xxx
Parkour
0.07
Parkour (Subskill: Sense of Balance)
H2H Combat: Parkour
The formula in cell B2 is:
=IFERROR(SUM(ARRAYFORMULA(IFERROR(VALUE(MID(FILTER(SPLIT(TEXTJOIN("",TRUE,filter(B$1,regexmatch(B$1,$A2)=TRUE)),"xxx"),SEARCH($A2,SPLIT(TEXTJOIN("",TRUE,filter(B$1,regexmatch(B$1,$A2)=TRUE)),"xxx"))),len($A2)+2,1000)),""))),"")
(Dragged down through the rest of the list) (Could not figure out how to make the formula "in line" on the question.)
Expected Results:
B2 = .07 (Working)
B3 = .1 (Not working)
B4 = Blank (Working)
The goal of the formula is to look into cell B1, and split everything out by "xxx". Then, filter the array of items with only exact matches with the line item in column A, then split again by the comma and add up those values. It worked for the first line item, but not the second. (Unsure why, but I strongly believe it has something to do with the parenthesis. When I removed the parenthesis from the name in Column A (and adjusted cell B1 to not have parenthesis), it worked. However, given the structure of the data, parenthesis are required, and I need to find a way for it to work with them.)
When I removed the IFERROR wrap around it in cell B3, I get this error note:
Function SUM parameter 1 expects number values. But " is a text and cannot be coerced to a number.
Any help is greatly appreciated.
You may find useful combining SPLIT with QUERY like this. It will group names and sum percentages:
=QUERY(INDEX (IFERROR(SPLIT(FLATTEN(INDEX(SPLIT(B1:B100,"xxx"))),","))),"SELECT Col1,SUM(Col2) where Col1 is not null group by Col1")
PS: invented a couple of extra line
UPDATE
I've thought you had another goal, try this formula. Having the previous chart generated by QUERY, I used VLOOKUP to match first column and return second one:
=INDEX(IFERROR (VLOOKUP(A2:A,QUERY(INDEX (SPLIT(FLATTEN(SPLIT(B1,"xxx")),",")),"SELECT Col1,SUM(Col2) where Col1 is not null group by Col1"),2,0)))

Using a concatenated string in Google sheets query WHERE condition

I am trying to use the result of a concatenation of a string and cell contents in a query, but the query function does not seem to like it: see this example
https://docs.google.com/spreadsheets/d/1CtPFLhpD3KDHrk-6WoHft4JdMTVa3OcvHHqAidMWwdM/edit?usp=sharing
The strings in cells B2 and B3 appear to be the same, but whilst C3 gives the desired result, C2 does not. B3 simply extracts a string from A3, whereas B2 tags the 'E' on in the formula. The data in the real spreadsheet I am working with is like that in B2, so I have to add the 'E' into the result. The real list of error codes is quite long and varied, so I cannot edit that to just use the number. I also tried using CONCATENATE, but that made no difference.
How do I get the query to recognise the contents of C2 as the string 'E151'?
Most likely there's an trailing space in the result of B2.
See if this helps
=query(A7:B10,"select B where A = '"&trim(B2)&"'",0)
In B2
="E"&mid(A2,30,3)
or
="E"&index(REGEXEXTRACT(A2,REGEXREPLACE(A2,"(\d+)","($1)")),1,5)
(the 5th number in the string)
delete everything in column C and use this in C2:
=INDEX(IFNA(VLOOKUP(TRIM(B2:B4), A7:B11, 2, 0)))
Some great answers to my question, thanks! It was a trailing space causing the problem.
I have combined some of the ideas in the other answers in the real spreadsheet and used
="E" & trim(mid(A2,30,3))
Since some of the error codes are only 2 characters long

Google Sheets: Find a Row that Matches Only a Few Specific Characteristics

I can't seem to find the right equation to find a cell from a row that matches only a few specific characteristics. In this example, I am trying to find the equation for Column D which would be the cell in A that has the same cells for B & C.
Hope this makes sense!
I'll provide two options.
If you're sure your data will only ever have zero or one match, you can place the following formula into D2 of an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,SUBSTITUTE(VLOOKUP(B2:B&C2:C,{B2:B&C2:C,A2:A},2,FALSE)&VLOOKUP(B2:B&C2:C,SORT({B2:B&C2:C,A2:A,ROW(A2:A)},3,0),2,FALSE),A2:A,"")))
However, if you think more than one match may turn up and you want "None" to be returned if there is no match, you can use the following formula in D2 or an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,REGEXREPLACE(REGEXEXTRACT(REGEXREPLACE(SUBSTITUTE(VLOOKUP(B2:B&C2:C,TRIM(SPLIT(FLATTEN(QUERY(QUERY({B2:B&C2:C&"~",A2:A&","}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),2,FALSE),A2:A,""),"^[,\s]+$","None"),"([^,\s].+[^,\s])[,\s]*$"),"[,\s]+",", ")))
The second formula will work even if there will only ever be zero or one match; it's just not necessary to have it be that lengthy. And the second formula is only as lengthy because it was unclear from your posted examples whether the data in Col A, B and C will really only ever be one word or not; so the formula is built to assume there will not always be one-word strings in those columns.
Either formula will provide results for the entire column without dragging.
Here's an option, You can use this formula in column D2:
=iferror(textjoin(", ",true,query($A$2:$C,"Select A where A is not null and A != '"&$A2&"' and B = '"&$B2&"' and C = '"&$C2&"'",0)),"None")
Limitation:
You need to manually drag the formula to its succeeding rows. Arrayformula() cannot be used in looping the query string values.
What it does?
Using query(), filter the data from A2:C that has the same current row last name(Column B) and food(Column C) at the same time having a different first name(Column A)
If there are multiple results, use textjoin() to combine them with ", " as its delimiter.
If there is no matched found, it will return an error, hence use iferror() to set the default value to "None"
Output

Sum not working in column beside arrayformula

I have a column which won't sum? it sits beside a column with an array formula how is a sum done in this case?
This is my formula in C3:
ArrayFormula(query({M8Report!A2:T,arrayformula(left(regexreplace(M8Report!N2:N, "\n|\r", ""),150))},"Select Col5,Col2,Col1,Col21,Col3 Where (Col4 = 'Work Order') order by Col5", -1))
in B I have entered integer values in B1 I have =sum(B3:B) and the result is always 0 if instead in B1 I use B3+B4+B5+etc.. I get the correct result...
..Tried everything I can think of and same issue sum =0
since C is dynamic I need a way to sum all of the values in B
..any ideas would be helpful
In your sample sheet your formula is:
= { QUERY ; { "TOTAL" , SUM(B3:B) } }
Change it to:
=CONCAT("TOTAL ", SUM(B3:B))
The error has to do with your use of the {}, which is used to define an array literal. You just want to have 2 strings merged where one is the sum of the values. Also note that you may want to use B4:B instead since B3 is a header for the data below that.
Last, make sure the data is Numbers. The original data is formatted as Plain Text so SUM() has nothing to add.
to ditch formatting issues you can do:
={"Total", SUMPRODUCT(B5:B)}

How to refer to column 'BY' in query?

In my Google spreadsheet, i'm using the query function to get data from one sheet onto another. The query looks something like this:
=QUERY('mySheet'!$A$1:100,"select F where "&C$3&"='myValue'")
This works fine until cell C3 has value "BY" (because the word "by" has significance in the query language). I've tried using single quotes, but then the query uses header "BY" instead of column BY and it returns an empty result.
Any ideas on how to work around this?
Put it in backticks.
=sum(QUERY(select `BY` where `BY` is not null limit 7))
This will sum the first 7 values in column BY.
(This was fun to debug. The formula worked in every other column...)
"BY" is a special word. It is present in clause group by
You may use this:
=QUERY({'mySheet'!$A$1:100},"select Col5 where Col"&C$3&"='myValue'", 0)
and paste the column number into C$3
see more info here
BTW you may use function column() to know BY is column 77 or find the right number by header name: =match("column name", 'mySheet'!1:1, 0)

Resources