I have range of cells and want to search with query formula where cells are not nothing.
A B C D
1 a 11 44 qw
2 b 12 r
3 c 13 44 444
4 NOT
5 f 15 55 88
6 NOT
7 h 17 gh ee
Cells in C maybe any number, maybe any word, maybe any nothing.
I want to select everything A where C has nothing.
Result must be:
a
b
c
f
h
When try all the querys formula:
=QUERY(A1:D7,"select A where not C<>'' ")
=QUERY(A1:D7,"select A where not C!='' ")
=QUERY(A1:D7,"select A where not C is null")
=QUERY(A1:D7,"select A where C !='' or not C is null")
Nothing give correct result. Looked everywhere.
What can I do?
Google QUERY function does not work here as "expected" because of mixed data types in column C. If we apply the formula =QUERY(A1:C7;"select A where C is not null";-1)
it will return only "a, c and f". The image below shows this result in column G, where G1 contains the above formula.
We should check data types more carefully, but it is difficult to do inside QUERY function. So I suggest to add one more column (E in our case) and fill it with "=TYPE(C1)" and similar functions. Having data types column, we can modify QUERY expression to take them into account. Final result is shown in column H.
Please check this approach again, if you are going to use other data types in column C.
=QUERY(A1:D7, "select A where C is not null")
...is what you need
Related
result
1
a
1
2
b
2
3
c
3
4
d
4
5
e
5
6
6
7
7
a
b
c
d
e
I want to list all the data in the range A:B as a return value in column C where the formula is written. Empty cells should be ignored.
I've tried getting a specific range of data with the indierct or indexof functions, but I have no idea how to concatenate them into one column in order if the target is more than one column!
You can try:
=LAMBDA(z,FILTER(z,z<>""))(FLATTEN(A:A,B:B))
Incase the new functions have rolled out for you, try:
=TOCOL(A:B,1,1)
I am trying to achieve the following functionality:
In every row look at the value of column B (for example) 1. Then run down the list summing the value of column C if the value of column B is also (for example) 1 and capture this in each rows column G.
So the expected result for rows 4,5 and 6's G would be 3,3 and 1 respectively.
I am thinking something like (this is pseudo)
=ArrayFormula(SumIf(RangeForB,THISROW=OTHERROWS,RangeForC))
I'm also not really sure how I would get "This Row", I know you can use ROW() but that will give me the index not the value of the row. INDEX() to my knowledge also isn't usable in ArrayFormula()
Help would be greatly appreciated, Cheers.
You can use SUMIF where the criterion is itself a range:
=ArrayFormula(filter(sumif(C4:C,C4:C,D4:D),C4:C<>""))
So the sumif is evaluated first for all of the rows where the values in column C match the number in C4 (rows 4 and 5), then for those matching the number in C5 (also rows 4 & 5), then those matching the number in C6 (row 6 only). The filter is needed to suppress the zeroes which would otherwise occur in rows 7 onwards.
=ARRAYFORMULA(MMULT(QUERY(B1:C,
"select B, C where B matches '1' and C matches '2'", 0),
(TRANSPOSE(COLUMN(B1:C1)^0))))
Suppose I've got a text values column (named Data), generated by =unique() function. Also, there is an array of patterns to find and replace for (Find and Replace columns).
Which formula should I use to scan each cell in Data for multiple patterns in Find and replace it, if match?
Data Find Replace Result
1 a c z a
2 b f y b
3 c e x z
4 d d
5 e x
6 c z
Tried =SUBSTITUTE() and =IF() functions, but it fails, when I set an array of patterns, instead of single one.
If the table you is in range A1:E7, try this formula
=TRANSPOSE(SPLIT(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(ARRAYFORMULA(CONCATENATE($B$2:$B$7&"|")),$C$2,$D$2),$C$3,$D$3),$C$4,$D$4),"|"))
You can read further about this in an older post and google docs forum.
Array solution
This formula takes range to replace, so it can be used for variable number of patterns:
=QUERY(ARRAYFORMULA({REGEXMATCH(A2,$B$2:$B$4),
REGEXREPLACE(A2,$B$2:$B$4,$C$2:$C$4)}),
"select Col2 order by Col1 desc limit 1")
or this one:
=INDEX(ArrayFormula(REGEXREPLACE(A2,$B$2:$B$4,$C$2:$C$4)),IFERROR(MATCH(A2,$B$2:$B$4,0),1))
or this:
=IFERROR(INDEX($C$2:$C$4,MATCH(A2,$B$2:$B$4,0)),A2)
The formula is need to be dragged down.
Single Formula & Array solution
Also this single ArrayFormula will do the trick:
=ArrayFormula(trim(transpose(query({IF(--REGEXMATCH(TRANSPOSE(A2:A7),$B$2:$B$4)=1,
REGEXREPLACE(TRANSPOSE(A2:A7),$B$2:$B$4,$C$2:$C$4),"");
TRANSPOSE(if(--not(REGEXMATCH(A2:A7,JOIN("|",B2:B4))),A2:A7,""))},,COUNTA(A2:A)))))
or this shorter formula:
=ArrayFormula(IFERROR(VLOOKUP(MATCH(A2:A7,B2:B4,0),{ROW(INDIRECT("a1:a"&COUNTA(C2:C4))),C2:C4},2,0),A2:A7))
Please, see explanations in Sample file
I'm having trouble figuring out a fairly simple QUERY statement in Google Spreadsheets. I'm trying to use a cell reference instead of static values and I'm running into trouble. Below it the code I'm using, but I keep getting a "Error: Formula parse error."
=QUERY(Responses!B1:I, "Select B where G contains"& $B1 &)
I'm sure it is a simple error, but can someone please show me how to write the above so the QUERY is pulling data from B where G contains the value in cell B1 (cell reference)?
Copied from Web Applications:
=QUERY(Responses!B1:I, "Select B where G contains '"&$B1&"'")
I only have a workaround here.
In this special case, I would use the FILTER function instead of QUERY:
=FILTER(Responses!B:B,Responses!G:G=B1)
Assuming that your data is on the "Responses" sheet, but your condition (cell reference) is in the actual sheet's B1 cell.
Hope it helps.
UPDATE:
After some search for the original question: The problem with your formula is definitely the second & sign which assumes that you would like to concatenate something more to your WHERE statement. Try to remove it. If it still doesn't work, then try this:
=QUERY(Responses!B1:I, "Select B where G matches '^.\*($" & B1 & ").\*$'") - I have not tried it, but it helped in another post: Query with range of values for WHERE clause?
I know this is an old thread but I had the same question as the OP and found the answer:
You are nearly there, the way you can include cell references in query language is to wrap the entire thing in speech marks. Because the whole query is written in speech marks you will need to alternate between ' and " as shown below.
What you would need is this:
=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' ")
If you then wanted to refer to multiple cells you could add more like this
=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' and G contains '"& B2 &"' ")
The above would filter down your results further based on the contents of B1 and B2.
I found out that single quote > double quote > wrapped in ampersands did work. So, for me it looks like this:
=QUERY('Youth Conference Registration'!C:Y,"select C where Y = '"&A1&"'", 0)
Here is working code:
=QUERY(Sheet1!$A1:$B581, "select B where A = '"&A1&"'")
In this scenario I needed the interval to stay fixed and the reference value to change when I drag it.
none of the above answers worked for me. This one did:
=QUERY(Copy!A1:AP, "select AP, E, F, AO where AP="&E1&" ",1)
To make it work with both text and numbers:
Exact match:
=query(D:E,"select * where D like '"&C1&"'", 0)
Convert search string to lowercase:
=query(D:E,"select * where D like lower('"&C1&"')", 0)
Convert to lowercase and contain part of the search string:
=query(D:E,"select * where D like lower('%"&C1&"%')", 0)
A1 = query/formula
yellow / A:B = result area
green / C1 = search area
blue / D:E = data area
If you get error when the input is text and not numbers; move the data and delete the (now empty) columns. Then move the data back.
Old Thread but I found this on my journey to the below answer and figure someone else might need it too.
=IFERROR(ArrayFormula(query(index(Sheet3!A:C&""),"select* Where Col1="""&B1&""" ")), ARRAYFORMULA({"* *","no cells","match"}));
Here is a simply built text Filter from a 3 column data set (A,B and C) located in "sheet3" into the current sheet and calling a comparison to a cell from the current sheet to filter within Col1(A).
This bit is just to get rid of the #N/A error if the filter turns up no results //ARRAYFORMULA({"* *","no cells","match"}))
Your question-title is very broad despite the question-body is asking for only one type of 'cell reference'. I'll try to answer some of the other scenarios in case other people come here looking for them.
A - String comparison with cell reference
=TRANSPOSE(QUERY(MIX!A16:F, "SELECT B,C,D,E,F WHERE (UPPER(A) = '"&F1&"') "))
=QUERY(MIX!A16:F, "SELECT B,C,D,E,F WHERE (UPPER(A) = '"&F1&"') ")
F1 = StringToSearch
B - String comparison with cell reference + SheetName is called using cell reference + Dates comparison using cell reference
F6 = SheetName
F2 and F3 = StringToSearch
I4 and I5 = dates with this format = 2022-01-01 = yyyy-mm-dd
If the dates you have are in a different format, you can use the formula below to convert them
I4 = TEXT(H5,"yyyy-mm-dd")
=QUERY(INDIRECT(F6&"!A2:C") , "select A,B,C where ((upper(B) contains '"&F2&"') or (upper(B) contains '"&F3&"')) AND (A >= date '"&I4&"') AND (A <= date '"&I5&"') ")
C - Data to query comes completely using cell reference
B1 = Sheetname!Range = Sheet1!A1:C
A36 = stringToFind
=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A36&"') ")
The "$" in $B$1 make the reference static for column and row, so if you copy/paste the CELL containing the formula (NOT the text of the formula), the A36 adapts accordingly, but B1 stays fixed.
D - Data to query comes completely using cell reference + String comparison using cell reference + Number comparison using cell reference
=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A40&"') AND C > "&A41&" ")
B1 = Sheetname!Range = Sheet1!A1:C
A40 = stringToFind = blah
A41 = Number = 100
E - Nested query (query inside query) + String comparison using cell reference + Number comparison using cell reference, using LIMIT and OFFSET
=QUERY( transpose( query(IMPORT!$A$7:AAL,"select * where A contains '"&$C34&"' ")) ,"SELECT * LIMIT " & $A34 & " OFFSET " & $B34, 0 )
The final '0' is there to NOT include the header in the results.
Given a spreadsheet with two columns, say A and B, each containing n values under it, all text; is there a formula that allows me to fill just one cell containing the amount of equal values in columns A and B?
Example:
A B
-----
1 M M
2 L M
3 L L
4 M M
5 M L
-----
3
Since columns A and B both contain an M in rows 1 and 4, and an L in row 3, the result is (i.e. 2+1).
A simple solution is to use QUERY function in google spreadsheet:
=SUM(QUERY(A1:B5, "Select 1 where A = B"))
Or using SUMPRODUCT:
=ARRAYFORMULA(SUM(((A:A)=(B:B)) * (1) ))
One of possible solution will be to add the following formula in column C: =N(EXACT(A1,B1)),
copy it throughout the column down to the last row and then sum up the column C values using =SUM(C1:C5).
Here we go:
=IF(EQ(LEFT(A0, 1), "A"),
SUM(ARRAYFORMULA(N(EXACT(TRANSPOSE(A1:A5), TRANSPOSE(B1:B5))))),
"")
Reading: if the value in row 0 (it doesn't exist, but my example above does ;) ) is equal to the text "A", take the sum of an array N, otherwise put in an empty string. ("")
Array N is build by taking the transpose of columns A and B. (Turning them, so they look like a row) and comparing the values. (Burnash gave me the options "N" and "EXACT") The formula N transforms this into a 1 or 0.
Copy paste the formula in an entire row and what do you know... It worked! That was hellish for something so trivial.
Thanks anyway.