Finding the most appearing value for each column a value - google-sheets

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"))

Related

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'

Using IF conditional in SELECT statement of Google Sheets Query function

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))

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…

What's wrong with my query? (Invalid query: Query parse error)

I'm trying to use a Google charts query in my spreadsheet to count the number of form responses by a specific person on a given date.
I'm very new to the Google Charts query language (I just started trying to figure it out about an hour ago), but I know SQL so I thought it wouldn't be too hard.
Anyway, here's what my query looks like right now:
=QUERY('Form Responses'!A2:F100, "select count(*) where name = 'Ajedi32' and date timestamp = date '2013-11-19'", {"timestamp", "name", "done", "todo", "blocker", "comment"})
And here's the error I'm getting:
error: Invalid query: Query parse error: Encountered " " * " " * "" at line 1, column 14. Was expecting one of: "min" ... "max" ... "avg" ... "count" ... "sum" ... "no_values" ... "no_format" ... "is" ... "null" ... "year" ... "month" ... "day" ... "hour" ... "minute" ... "second" ... "millisecond" ... "with" ... "contains" ... "starts" ... "ends" ... "matches" ... "like" ... "now" ... "dateDiff" ... "quarter" ... "lower" ... "upper" ... "dayOfWeek" ... "toDate" ... ... ...
I don't get it. Why would it be expecting a function name instead of *?
I think the problem is likely that count(*) is not a valid construct in the Query language. Try using a column name instead of *.
[edit - answer pertains to a different API; new answer below]
After reading over some questions from other users (link1, link2, [link3(Google spreadsheet Query Error - column doesn't exist)), it seems like the QUERY function does not support the use of column names. You can either refer to columns by letter:
=QUERY($A$1:$C$10, 'select A, B, C');
or by column index in the selected range:
=QUERY($A$1:$C$10, 'select col1, col2, col3');
Try that and see if addressing your columns by letter or index works.
Ultimately, I ended up avoiding the query function entirely by using the filter function instead:
=COUNT(FILTER('Form Responses'!$A$1:$F$100; DATEVALUE('Form Responses'!$A1:$A100)=DATEVALUE('2013-11-19'); 'Form Responses'!$B1:$B100='Ajedi32'))
Expanded for readability:
=COUNT(
FILTER(
'Form Responses'!$A$1:$F$100;
DATEVALUE('Form Responses'!$A1:$A100)=DATEVALUE('2013-11-19');
'Form Responses'!$B1:$B100='Ajedi32'
)
)
(If anyone still wants to have a go at answering my original question, feel free.)

Resources