Query is ignoring string (non numeric) value - google-sheets

I have this simple google sheet :
In C2, i use simple query :
=query({A2:A15},"select Col1")
But all the non-numeric value has been skipped. Why is it ?

As MattKing already mentioned in the comments, query doesn't like mixed data type in a single column.
This is simply because, for query purposes, the majority data type determines the data type of the column. The minority data types are considered as null values.
There are still ways to circumvent the issue. Here are the possible alternatives:
Force the data to be treated as a single data type. (in this case, you can use to_text)
=Arrayformula(query({to_text(A2:A15)},"select Col1"))
Using filter (MattKing)
=filter(A2:A15, A2:A15 <> "")
Query with 1 as header (Broly)
=query({A2:A15},"select Col1", 1)
Wrapping the range with arrayformula if you don't have any filtering to do.
=arrayformula(A2:A15)
Output:
Reference:
Query Mixed Data Type Issue

try:
=INDEX(QUERY({A2:A15}&"", "select Col1"))
UPDATE:
if QUERY is not able to evaluate mixed data then the only way how to tackle it is to convert input into plain text. this can be done in 5 ways:
appending trailing empty space:
=ARRAYFORMULA(QUERY(A:C&"", "select *", 0))
wrapping it into {}:
=ARRAYFORMULA(QUERY({A:C}, "select *", 0))
using formula:
=ARRAYFORMULA(QUERY(TO_TEXT(A:C), "select *", 0))
or partial combination. for example, if we know that column B is problematic but A and C are fine and shouldn't be converted we can do:
=ARRAYFORMULA(QUERY({A:A, TO_TEXT(B:B), C:C}, "select *", 0))
or formatting the source range as Plain Text
worth mentioning that either of these top 4 ways you use you will need ARRAYFORMULA wrapping and column references in SQL command needs to be Col1,Col2,Col3 instead of A,B,C
"yes, but I need outputted numeric values for further calculations"
nope, (in 99/100 cases) there is no need to actually have numeric values because by doing the calculation itself will convert automatically the plain text formatting into numeric values. example:
="123" + "2"
if there is a special case when you totally need numeric values there is IFERROR workaround:
=ARRAYFORMULA(IFERROR(QUERY(TO_TEXT(A:C), "select *", 0)*1,
QUERY(TO_TEXT(A:C), "select *", 0)))
by repeating the same thing twice this can get really long in some cases but it works. fortunately, this can be nicely refactored with LAMBDA like:
=ARRAYFORMULA(LAMBDA(x, IFERROR(x*1, x))
(QUERY(TO_TEXT(A:C), "select *", 0)))

Related

How to extract the data on the right-hand side of the arrows in google sheet?

Hi everyone,
I want to extract the values after the arrows. If there is no arrow, then the expected output will be the value in the cell. I know that this can be done by using few functions (REPLACE or SUBSTITUTE) in another column, but I want to make this happen inside the QUERY function or wrap the QUERYfunction inside some functions so that I'm not require to have extra column to get the values after the arrow (if available). I'm not sure whether this can be achieved.
I tried to wrap the QUERY function inside a REPLACE function but this doesn't work.
The value in column F is the expected output that I want. Appreciate for any feedback or suggestion, thank you!
See if this helps
=INDEX(
REGEXEXTRACT(
QUERY(
IMPORTHTML(
"https://www.marketbeat.com/stocks/NYSE/NKE/price-target/", "table", 2),
"Select Col5 where Col5 is not null and Col1 > date
'"&TEXT(DATEVALUE(B2), "yyyy-mm-dd")&"'", 0),
"\$?[0-9.]+$")
)
Note that the output will be strings (text). To convert to number, change
"\$?[0-9.]+$")
to
"\$?[0-9.]+$")+0
and format the output column as 'valuta'.

Google Sheets Query returning odd formatting

I have a simple sheet to try to track and format race results from a league that I've joined. For the most part I know how I want to do this but when I use a query it's dropping data in some situations and formatting it strangely in others.
It seems as if where there are more numbers in a column than text it drops all text entries.
In addition for some reason when I add a check row, if it's included in the query it pushes almost all the data into a single cell except for the check row.
Would someone mind having a look and trying to figure out why it's doing this. Link Below
On sheet RRL1 I have my compiled data on the left, my 'missing' data on the right and my weirdly formatted data below.
https://docs.google.com/spreadsheets/d/1c9xlQG06dQCrpMk3UMAX29oTlpRuhTfx6btbYTGmC8g/edit?usp=sharing
The query() formula will only support one data type per column — number, text, boolean or date. The type is determined by the majority of the values in the first few hundred rows. Values that are of another type will be returned as null, i.e., blank values.
=QUERY('Tournament Details'!D2:E22)
Use an { array expression } like this:
={ 'Tournament Details'!D2:E22 }
=TRANSPOSE(query('Tournament Details'!I3:I26))
Use this:
=transpose('Tournament Details'!I3:I26)
Use this pattern to replace "DNS" and "DNF" with nulls:
=arrayformula(
query(
{ 'RRL1'!A1:C, iferror(value('RRL1'!D1:D)) },
"select Col3, sum(Col4)
where Col3 is not null
group by Col3
label sum(Col4) 'Total AUS RRL1' ",
1
)
)
The "squished" values you mention come about because you are not specifying the headers parameter. The best practice is to always include it, like this:
=query('Tournament Details'!A2:E22,"select A where C != 'N/A'", 1)

Google Sheets bug? query completed with an empty output

I have a named range in Google Sheets and I am making a simple query which should return a value but it doesn't work:
=QUERY(range2,"SELECT B WHERE A contains 'test' ")
It should return "2" but it sais that the query completed with an empty output.
Is it a bug?
actually, QUERY is known to behave like this and its caused by a superiority of numeric values over text string values in a given dataset. fix would be to select columns A and B and format them as Plain text
another approach would be to convert dataset within formula like:
=ARRAYFORMULA(QUERY(TO_TEXT(A:B),
"SELECT Col2
WHERE Col1 contains 'test'"))

How to maintain the original header name when performing Google Sheets Query

Hi I have this working query that loops through an arbitrary range of data and produces the results I need:
=arrayformula(QUERY(Crew!A:DY,"SELECT E,A," & join(",", substitute("Count(`1`)", "1",substitute(address(1, column(Crew!F:DY), 4), "1", ""))) & "GROUP BY E,A", 1))
Unfortunately, this produces Columns that are labeled so:
count 18/12/2017 count 25/12/2017 count 01/01/2018
I need to force a display of the original column name, and if possible, the format, e.g. 18/12/2017 as this will allow me to perform further pivot or group by month type functions
I have experimented with different methods of adding a label at the end of the query, reduced the query to tests of the data without using the arrayformula, and searched through the queryLanguage Docs but all references seem to be related to applying a different text string rather than leaving the column header 'raw'
I suspect the main problem is my inexperience, I don't know the correct terms to search for?
How do I achieve this?
Cheers.
You need to use label construction:
"select ... where ... group by ... label ..."
Reference:
https://developers.google.com/chart/interactive/docs/querylanguage#label
You may get labels text with the formula:
="label "&ArrayFormula(join(", ",substitute("count(`1`) ", "1",substitute(address(1, column(F1:G1), 4), "1", ""))&text(F1:G1," 'dd/mm/yyy'")))
change F1:G1 to your range.
The shorter version of the formula with Regex:
="label "&ArrayFormula(join(", ",REGEXREPLACE(ADDRESS(1,COLUMN(F1:G1),4),"(.*)\d+$","count(`$1`) "&TEXT(F1:G1,"'dd/mm/yyy'"))))

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