Using IF conditional in SELECT statement of Google Sheets Query function - google-sheets

In order to cleanse some outlying bad data I need to implement a conditional statement into the SELECT statement of a query in Google Sheets, but GS does not want to cooperate
My attempted statement:
=QUERY('JSON Data'!A1:AS,"Select A, IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) where B is not null GROUP BY A pivot B label IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) 'ratio' ",1)
Error thrown:
Unable to parse query string for Function QUERY parameter 2:
PARSE_ERROR: Encountered " "(" "( "" at line 1, column 13. Was
expecting one of: "where" ... "group" ... "pivot" ... "order"
... "skipping" ... "limit" ... "offset" ... "label" ... "format" ...
"options" ... "," ... "" ... "+" ... "-" ... "/" ... "%" ... "" ...
"/" ... "%" ... "+" ... "-" ...
Note that the following statement works:
Select A, SUM(AP)/SUM(AS) where B is not null GROUP BY A pivot B label SUM(AP)/SUM(AS) 'ratio'
The following statement does not work (testing to see if labelling the field has an effect):
Select A, IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) where B is not null GROUP BY A pivot B
The sheet is here: https://docs.google.com/spreadsheets/d/1raQI22n3J08nKCHnz2LxRCXRP3dDnYJtGCEb8o4VUWY/edit#gid=1466522015
How to (Can I) execute a proper IF/THEN in the SELECT portion of a Google Sheets QUERY statement?

Try to process the if then before the query. See if this works
=ArrayFormula(QUERY({'JSON Data'!A1:B, IF('JSON Data'!AP1:AP/'JSON Data'!AS1:AS > 1, 1, 'JSON Data'!AP1:AP/'JSON Data'!AS1:AS) } ,"Select Col1, sum(Col3) where Col3 is not null GROUP BY Col1 pivot Col2",1))

Related

Finding the most appearing value for each column a value

I have a sheet with 2 columns, A which is a group name and B which is a Role name.
Each Group appears multiple times with different roles, I want to find the most appearing role value in Column B for each Group value in column A.
How can I achieve it using Google query ?
I didnt manage to succes,
i tried using this google query :
=QUERY(A1:B, "SELECT b GROUP BY A HAVING COUNT(B) = MAX(COUNT(B))",1)
but I get an error:
Unable to parse query string for Function QUERY parameter 2:
PARSE_ERROR: Encountered " "HAVING "" at line 1, column 21. Was expecting one of: "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "," ... "" ... "+" ... "-" ... "/" ... "%" ... "" ... "/" ... "%" ... "+" ... "-" ...
You can do a QUERY to count the number of appearences, but then (at least with the tools I have) you should filter the values to find the different MAX combinations:
The query being:
=QUERY(A:B,"SELECT A,B,COUNT(B) GROUP BY A,B")
And the complete formula:
=LAMBDA(quer,MAP(FILTER(UNIQUE(A:A),UNIQUE(A:A)<>""),
LAMBDA(each,SORTN(FILTER(quer,INDEX(quer,,1)=each),1,0,1,1))))
(QUERY(A:B,"SELECT A,B,COUNT(B) GROUP BY A,B"))

Google Sheets, Select all WHERE cell matches value in a column

I am querying data from a sheet and want to only display the results where column A from that sheet matches the values in a column on the current sheet.
Error Message
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " <UNEXPECTED_CHAR> "! "" at line 1, column 34. Was expecting one of: "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "and" ...
Here is what I tried:
=QUERY(DFSdata!1:975,"SELECT * WHERE A MATCHES BetMaker!&A1:A15'",1)
=QUERY(DFSdata!1:975,"SELECT * WHERE A MATCHES 'BetMaker!&A1:A15''",1)
=QUERY(DFSdata!1:975,"SELECT * WHERE A MATCHES 'A1:A15'",1)
You can try by referring to those values joined:
=QUERY(DFSdata!1:975,"SELECT * WHERE A MATCHES '"&TEXTJOIN("|",1,BetMaker!&A1:A15)&"'",1)

Query & importrange with a word that has symbol ' (example = James 'Lee)

I have issue to list the training program with the name that has symbol '. The participant name is James 'Lee
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets...", "'record'!A:Z"), "Select Col7,Col18 where Col1 = '"&C5&"' and Col1 is not null", 0)
enter image description here
I received the error message:
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "Lee "" at line 1, column 40. Was expecting one of: "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "and" ... "or" ...
Try
=QUERY( IMPORTRANGE ("..., "...."), "Select Col7, Col18 where Col1 = """&C5&"""", 0)
and see if that works?
Select Col7,Col18 where Col1 like '"&regexreplace(C5,"\'","\%")&"' and Col1 is not null
will remove the hyphen and replace it with a wild card character %.
The side effect is that it will also bring back results like 'James OtherLee'

Google Sheets query, unable to decipher error message

I am trying to copy data over from one sheet to another using Google Sheets and am using this query in the second sheet
=query(IMPORTRANGE("5UAxxxxxxxxxxxx-GMcxccccccccp-xsdff0","SheetName!A4:AI1000"),"select Col1, Col2, Col3, Col5, Col7, Col9, Col14, Col16, Col 18, Col19, Col20, Col21 where Col35 = 'Yes'")
Unable to understand what this error means.
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " <INTEGER_LITERAL> "18 "" at line 1, column 62. Was expecting one of: <EOF> "where" ... "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "," ... "*" ... "+" ... "-" ... "/" ... "%" ... "*" ... "/" ... "%" ... "+" ... "-" ...
My Sheet1 where I am copying data from has columns A-Z and AA-AI, so I have A4:AI1000 to capture all the data assuming we won't have more than 1000 records with the headers taking up from 1-3 rows.
A simple one, you have a space between in the column reference Col 18.
The error message was pretty clear:
QUERY Parameter 2 is the query string itself
Encountered " <INTEGER_LITERAL> "18 "" means that it found the number 18 when it was expecting either a column reference or an SQL clause…

Summing names with same values

I have an spreadsheet with one column with a bunch of names ( with duplicates) and a testing column which will be either 'ok', 'not - ok' or '' (if not started). I wanted to create a formula that would get all the unique names and then count how many 'not - ok' + '' that corresponds to that name so eg
Column A Column B
Bob ok
John not - ok
Rob
Bob not - ok
John ok
Joe ok
John
And the desired output would be
Column C Column D
Bob 1
John 2
Rob 1
Joe 0
I was able to get the unique name with
=UNIQUE(A2:A10) but not sure how to generate column D
this query gives everyone with 'ok' matched in column B:
=query(A2:B, "select A, count(B) where B matches '.*ok' group by A", 0)
if you like to make custom headers in this query, use this formula:
=query(A2:B,
"select A, count(B) where B matches '.*ok' group by A label A 'name1', count(B) 'name2'",
0)
Maybe try something like this:
=query(A2:B, "select A, count(B) where A <> '' and B <> 'ok' group by A", 0)
or
=query(A2:B, "select A, count(B) where A <> '' and (B = '' or B = 'not - ok') group by A", 0)
You can do it with pivot. "Names" goes in rows, as value you can use count of "column B" elements. Then add a global filter for the "ok" status

Resources