Google Sheets QUERY statement: order by average - google-sheets

I have this Google Sheet with a query formula in it. I am trying to use the query to display an average 1-5 satisfaction rating for each program. This script I adapted from Info Inspired - see "formula 1" - is basically working using the following formula in cell A2:
=query(ArrayFormula('Form Responses 1'!B2:E),"Select B,Avg (E) group by B label Avg(E)''")
However, this is displaying the results ordered alphabetically by program.
How can I order the output by the average rating currently in column B of the image?
I tried using the order by clause as in:
=query(ArrayFormula('Form Responses 1'!B2:F),"Select B,Avg (F) group by B label Avg(F)'' order by Avg(F)")
and
=query(ArrayFormula('Form Responses 1'!B2:F),"Select B,Avg (F) group by B label Avg(F) order by Avg(F)''")
but both gave me:
Error Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "order" "order "" at line 1, column 44. Was expecting one of: "format" ... "options" ... "," ...
I am not sure what the '' is doing, but the first query mentioned only works with it in there.

try it like this:
=QUERY(QUERY(ARRAYFORMULA('Form Responses 1'!B2:E),
"select B,Avg(E) group by B label Avg(E)''", 0), "order by Col2 asc", 0)
'' is literally nothing eg. empty label

Related

Combining Query and If Statements in Google Sheets

I need to know if this is possible:
I have a data in Google Sheets. I want to do a query on the data itself.
I want to be able to query any data that has a certain cell filled in with a date format. If the cell is filled in with a date format, then the data is transferred over to the query but if it does not contains date data in the cell, then the data won't get queried over.
So Is it possible to put a nested if statement in Query?
For example this is the workflow of the query
Check if Date cell is filled with data >> Check for the Broker name >> Select Column B and F to show in the query
Below is my Query formula so far:
=QUERY({Funded_2022!A:AM, if(Not(IsBlank(W:W)))}, "Select B, F")
and its coming with the error message:
Function ARRAY_ROW parameter 2 has mismatched row size. Expected: 2002. Actual: 1
try:
=QUERY({Funded_2022!B:B, Funded_2022!F:F, Funded_2022!W:W},
"select Col1,Col2 where Col3 is not null", )

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

Exclude Header QUERY

I want to construct a Query that takes data from multiple sheets and lists the contents of them all. All sheets have the same variables and should, therefore, be combined. When typing in 0 as the last argument of the query I always receive an error.
I am using the following formula and have tried to input 0 as well as FALSE as the last argument. Every time I received an error. In addition to this, I tried including label AI '' at the end of the statement which worked but I could not seem to also have a blank for AJ. Furthermore, I suspect that the blank spaces will still be shown which I don't want.
={QUERY(PRIO!A:AX, "select D, E where B matches 'Negative' order by D");
QUERY(collated!A:AJ, "select AI, AJ where AG matches 'Negative' order by AJ")}
not sure what went wrong because you didn't provide a copy of your sheet but try:
={QUERY(PRIO!A1:AX, "select D, E where B matches 'Negative' order by D", 1);
QUERY(collated!A2:AJ, "select AI, AJ where AG matches 'Negative' order by AJ", 0)}

How do I GROUP BY for only one column, while still being able to sort by and select other columns?

I have a sheet holding raw data that I would like to filter and sort in another sheet.
Raw data sheet
The goal of the sheet is:
Show only the highest Rank per Name at Level 12 or below
Sort the results from highest to lowest Damage
I show and sort all items with a Level of 12 or below using ORDER BY:
=ARRAYFORMULA(QUERY(RAW!2:1000, "select A,B,D where C<=12 and A<>'' order by D desc"))
Sorted by Damage
But this still shows too many entries. I can filter the entries using MAX() and GROUP BY:
=ARRAYFORMULA(QUERY(RAW!2:1000, "select A,max(B) where C<=12 and A<>'' group by A label max(B) ''"))
Filtered to only show highest rank
However, I cannot sort by DAMAGE or else I get this error:
Unable to parse query string for Function QUERY parameter 2:
COL_IN_ORDER_MUST_BE_IN_SELECT: `D`
And attempting to add that column to the SELECT clause gives this error:
Unable to parse query string for Function QUERY parameter 2:
ADD_COL_TO_GROUP_BY_OR_AGG: D
The error means that I cannot include fields that are not either aggregate formulas (like MAX) or are not a part of the GROUP BY (which DAMAGE is not).
My goal is this:
Filtered to only show highest rank and sorted by damage
How would I go about querying the first sheet to end up with the above sheet?
Here is the actual sheet. Please feel free to fork if you'd like to test an solution.
https://docs.google.com/spreadsheets/d/1XG3eTSc-8eYRh-6ekq_2BjZgleUVCz3v45PLozXe8Xo/edit
If you work from your query (arrayformula is not needed)
=QUERY(Raw!2:500, "select A,B,D where C<=12 and A<>'' order by D desc")
You can filter the query results to get your desired output:
=FILTER(A2:C,MMULT((A2:A=TRANSPOSE(A2:A))*(B2:B<TRANSPOSE(B2:B)),SIGN(ROW(B2:B)))=0)
I have not had success combining the two.
This is possibly a trivial answer, but might help to clarify your requirements. In the case of your test data where max(damage) is in the same row as max(rank), you can just put
=ARRAYFORMULA(QUERY(Raw!2:1000, "select A,max(B),max(D) where C<=12 and A<>'' group by A order by max(D) desc label max(B) '', max(D) ''"))
If what you want is to sort in general by 'the value of damage that corresponds to the highest rank' then the answer is more complicated!

Conditional formatting with QUERY result in Google Spreadsheet

I have a spreadsheet with 1 sheet of software version an another sheet of installation records. I want to do a conditional formatting that compares the version of in installation (on column F) to its know latest version number on another sheet ('Software Versions').
Current Solution
I came up with this formular initially:
=AND(F2<>"", F2=G2)
It works. But I need to maintain a column of QUERY results on G2:
=QUERY('Software Versions'!$A$1:$B$8, "Select B where A='" &D4& "' LIMIT 1")
Problem
Now I want to remove the G2 row altogether. I came up with this combined query:
=AND(F2<>"", F2=QUERY('Software Versions'!$A$1:$B$8, "Select B where A='" &D4& "' LIMIT 1"))
But I cannot save it because it is an "Invalid Formula":
Any way to actually do it?
Try use this formula:
=AND(F2<>"", F2=IFERROR(QUERY(INDIRECT("'Software Versions'!$A$1:$B$8"),
"Select B where A='" &D4& "' LIMIT 1 label B ''"),""))
I was able to make conditional formatting with this formula.
Improvements
Use indirect to address another sheet
Use label B '' to prevent header appear as query result
Use iferror(..., "") to be sure no error occurs when no data is found with query
I'm used to the good old VLOOKUP function for my conditional formattings.
The syntax is VLOOKUP(valueOrCell, sourceRange, index, FALSE)
So if the value from first parameter valueOrCell, exists in the sourceRange, return the value at index. FALSE is to have the exact match.
In my example I'm using only a single worksheet, but if you set the sourceRange to different worksheet it works as well.

Resources