Query should exclude headers - google-sheets

I'm trying to get just one result from this query:
=QUERY('Registro Clinico'!A1:AA1000; "select A where(B='B12')")
where B12 contains 17.555.829-2.
I need it to return the ID of the patients, but it's returning the header name "ID".

You can exclude headers by supplying FALSE for the headers parameter. (Documentation)
=QUERY('Registro Clinico'!A1:AA1000; "select A where (E='"&B12&"')"; FALSE)
^^^^^
You had other problems as well:
The Query language does not support references into the spreadsheet in the way you had tried to write it.
"...where (B='B12')..."
Instead, you need to concatenate text segments using the & operator:
"...where (B='" & B12 & "')..."
^^^ ^^^
The column in 'Registro Clinico' that contains 17.555.829-2 is E, not B.

This should do the trick:
=QUERY('Registro Clinico'!A1:AA1000,"select A where E='" & 'Registro Clinico'!B12 & "'", FALSE)

Related

Using Query to select column based on the headers

I have a dynamic table where columns are moved every now and then. Therefore, I would like to reference my column name in my query. Unfortunately I do not know so well and the internet raises more questions.
My formula looks like this:
`=query('X Source'!A:AP, "select D, E, AA, AM, X, A where "&if(month(now())=1,"(month(A)<11)","(month(A) <=month(now())-2)")&" and (V like 'C & G' or V like 'SAS' or V like 'SXS D' or V like 'DIR') Order By A desc")
D = Cinter
E = Cluster
AA = Creation Date
AM = Change Ow
X = Title
A = Date`
Do you have any idea ? I would like not to write a script.
I have already tried with the function filter to bypass but there I get no further because of the filtering after month.
`={FILTER('X Source'!AA:AA, 'X Source'!V:V="SAS",'X Source'!X:X<>"%BY SB%",'X Source'!X:X<>"%SB ONLY%", month('X Source'! AA:AA)=month(today())-1);FILTER('X Source'!AA:AA,'X Source'!V:V="SXS D",'X
Source'!X:X<>"%BY SB%",'X Source'!X:X<>"%SB ONLY%"`
You can define the following Named Function. (Data > Named functions > Add new function)
Name:
BETTERQUERY(range, better_query, headers)
Definition
=QUERY({range},IF(IFERROR(SPLIT(better_query,"`")=better_query,1),better_query,
REGEXREPLACE(REDUCE(better_query,REGEXEXTRACT(better_query,REGEXREPLACE(
REGEXREPLACE(better_query,"([()\[\]{}|\\^$.+*?])","\\$1"),"`(.*?)`","`($1)`")),
LAMBDA(acc,cur,SUBSTITUTE(acc,cur,IFNA("Col"&MATCH(cur,INDEX(range,1),0),cur)))),
"`(Col\d+)`","$1")),headers)
And then use it like this:
=BETTERQUERY('X Source'!A:AP',"select `Cinter`, `Cluster`, `Creation date` ...",1)
For more information on how this works see How to Use Column Names in QUERY
You could use MATCH to find the numbers of columns, and grab your range in curly brackes so you can refer them as Col1,Col2,Col3 instead of A,B,C
Just to make it more dinamic and you could change your range, I wrapped it in LAMBDA. With the headers I matched all your values and joined them with comma. 'date' only matched that colum. Column V I had my doubts about if it was a mistake when you said which columns was located at which headers. Please change "Title" in ""Col"&MATCH("Title",INDEX(range,1),0)" to the actual desired header title (that now is in V column) so it matches correctly:
=LAMBDA(range,
LAMBDA(headers,date,title,query({range}, "select "&headers&" where "&if(month(now())=1,"(month("&date&")<11)","(month("&date&") <=month(now())-2)")&" and ("&title&" like 'C & G' or "&title&" like 'SAS' or "&title&" like 'SXS D' or "&title&" like 'DIR') Order By "&date&" desc"))(
JOIN(",",INDEX("Col"&MATCH({"Cinter","Cluster","Creation Date","Change Ow","Title","Date"},INDEX(range,1),0))),
"Col"&MATCH("Date",INDEX(range,1),0),
"Col"&MATCH("Title",INDEX(range,1),0)
))
('X Source'!A:AP)
In my dummy example with random columns, the inside part of the query would look like this:
"select Col7,Col3,Col15,Col20,Col11,Col12 where (month(Col12)<11) and
(Col11 like 'C & G' or Col11 like 'SAS' or Col11 like 'SXS D' or Col11
like 'DIR') Order By Col12 desc"
UPDATE with other Locale
=LAMBDA(range; LAMBDA(headers;date;pl;query({range}; "select "&headers&" where "&if(month(now())=1;"(month("&date&")<11)";"(month("&date&") <=month(now())-2)")&" and ("&pl&" like 'C & G' or "&pl&" like 'SAS' or "&pl&" like 'SXS D' or "&pl&" like 'DIR') Order By "&date&" desc";1))( JOIN(",";INDEX("Col"&MATCH({"Cinter";"Cluster ";"Creation Date";"Change Owner";"Title";"Date"};INDEX(range;1);0))); "Col"&MATCH("Date";INDEX(range;1);0); "Col"&MATCH("PL";INDEX(range;1);0) )) ('X Source'!A1:AS)

Try to use query to vlookup and filter a set of raw data

I try to run this query:
=Query({'raw_13-19'!A:G},"Select * where C ={'Keyword'!G2:G19}", 1)
But it keeps returning:
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " <ID> "C "" at line 1, column 16. Was expecting one of: "(" ... "(" ...
I'm not quite sure why is this happening. Can anyone help?
Since the range is within {}, you need to use the other select notation 'Col1,Col2,Col3' instead of 'A,B,C'.
Also {'Keyword'!G2:G19} won't work, but you should use textjoin() instead.
Try:
=Query({'raw_13-19'!A:G},"Select * where Col3 matches '"&textjoin("|",1,Keyword!G2:G19)&"' ", 0)
If you have a header column on raw_13-19, then change , 0 at the end to , 1.
NOTE: the above will work with text fields, but if you have any issues, please share a mockup sheet so we can see the actual data scenario.

QUERY - Google Sheets - filter with conditions

I'm trying to filter some rows of this sheet
Google sheet sample image
I'm using the following query:
=query(Jira_query!$A:$I;"SELECT A WHERE E contains '&SDP&' AND C IN ('To Do','In Progress')")
But I'm only getting this error:
Error
PARSE_ERROR: Encountered " "C "" at line 1, column 39. Was expecting one of: "(" ... "(" ...
I would appreciate some help here, thanks!
Get rid of the ampersands before and after SDP. That is a string literal and should just be sitting between the single quotes.
In addition, I don't think QUERY supports IN; instead, use MATCHES with a pipe-separated list.
Finally, you only need to reference A:E in the QUERY, since you don't reference any columns beyond E in the SELECT clause.
So try this:
=QUERY(Jira_query!$A:$E;"SELECT A WHERE E CONTAINS 'SDP' AND C MATCHES 'To Do|In Progress'")

IFS statement with multiple nested QUERY functions

With a fair amount of help from SO folks, I managed to put together about 15 QUERY tabs used to extract necessary data for reporting.
In order to make it tidy, I'd like to enclose all these queries in an IFS statement referring to a Data Validation cell and run the QUERY by simply changing the value from the validation cell.
I managed to get it running with no errors, however, I get a single cell output.
What I am using for this is:
=IFS('Validation'!$A$1 = "Giberish",
{QUERY('All'!$A$5:$AP,
"SELECT AJ, '" & IDBlah & "', 'Blah'
WHERE AJ IS NOT NULL
LABEL '" & IDBlah & "' 'ID',
'Blah' 'BlahBlahBlah'",1)},
'Validation'!$A$1 = "GiberishGiberish",
{QUERY('All'!$A$5:$AP,
"SELECT AJ, 'WCaa', 'Blah'
WHERE AJ IS NOT NULL
LABEL 'WCaa' 'BlahBlah',
'Blah' 'BlahBlahBlah'",1);
QUERY('Upload'!$A$2:$AL,
"SELECT A, 'WCaa', 'Blah'
WHERE A IS NOT NULL
LABEL 'WCaa' '',
'Blah' ''",0)} )
I tried to enclose it all in an ARRAYFORMULA, but I get the same single cell output, with no error.
What is the correct approach?
Let me know if you need dummy data, but have in mind that I have corporate security policies that restrict me from sharing any sheet to anyone outside of the company.
you will need to use IF instead of IFS like:
=IF(Validation!$A$1 = "Giberish", {QUERY(All!$A$5:$AP,
"SELECT AJ,'"&IDBlah&"','Blah'
WHERE AJ IS NOT NULL
LABEL '"&IDBlah&"''ID','Blah''BlahBlahBlah'",1)},
IF(Validation!$A$1 = "GiberishGiberish", {QUERY(All!$A$5:$AP,
"SELECT AJ,'WCaa','Blah'
WHERE AJ IS NOT NULL
LABEL 'WCaa''BlahBlah','Blah''BlahBlahBlah'",1);
QUERY(Upload!$A$2:$AL,
"SELECT A, 'WCaa','Blah'
WHERE A IS NOT NULL
LABEL 'WCaa''','Blah'''",0)}, ))
when IFS is in a combination of ARRAYFORMULA it gains a special skill which requires an array to be inputted in order to output an array again.
https://webapps.stackexchange.com/a/124685/186471

Combining two fields into one hyperlink

So I have two columns in my database, "URL" and "Title".
The first one contains bare URL in the form of http://something.somewhere.com, the second is the descriptive name of it (quite intuitive).
I need to write a query which would combine these two columns into one "URL" like that A descriptive name.
I've tried with the # symbol this way:
descriptive_name#url#
and it works if you input it manually, but apparently it doesn't work with queries.
I've tried:
UPDATE table SET table.[URL] = (SELECT [Title] & "#" & [URL] & "#" FROM table);
Your UPDATE query syntax is wrong.
It should be:
UPDATE table SET table.[URL] = [Title] & "#" & [URL] & "#"

Resources