Google query multiple columns references - google-sheets

I have this query:
=QUERY(all!A:Z, "select B where (B=1 and H=true)")
How can I turn B and H into a column reference so I don't have to write them when I copy the query to new cells.
Note: column B is a number, meanwhile H contains boolean values.

try it like this:
=QUERY({all!A:Z}, "select Col2 where (Col2=1 and Col8=true)")

Related

Need to know with formula in Google sheet

I have date in column A, Name in column B and Product sale data in column C. Now I want formula which gives me in return in another table, If that person has sold something on a particular date that sale data entry is shown
Do it with a pivot table so that you will have the answer without any formula.
Try query() with a pivot clause, like this:
=query(A1:C, "select A, B, count(B) where A is not null group by A, B pivot C", 1)
May be it can help too
=Query('Sales '!A1:C, "Select Col1, Col2, Col3 Where Col3 is not null",0)
'Sales ' is data source worksheet
use ,1 if you don't want to include the same headers of of your data source worksheet

Google Query SELECT statement concatenated with a NESTED IF result

Is it possible to return a Nested IF result from a CELL that will be concatenated to the SELECT statement in the QUERY function?
For example, I am trying to return the result for the following Nested IF function into the Query Function:
https://docs.google.com/spreadsheets/d/15i1E8AZHORRmPlu1VQqFRN1_7-aUyAz-hlYMOUtIlY4/edit?usp=sharing
Appreciate it, if anyone could take a look.
Regards
JVA
its done like this:
=QUERY(TESTDATA!A1:D16, "SELECT A, D, SUM(C) WHERE 1=1 "&
IF(AND(M3="NAME",N3="Customer"), " GROUP BY A, D PIVOT B",
IF(AND(N3 = "Customer"," AND A = '"&M3&"' GROUP BY A, D PIVOT B"),
" AND A = '"&M3&"' GROUP BY A, D PIVOT B",
" AND A = '"&M3&"'
AND D = '"&N3&"' GROUP BY A, D PIVOT B")), 1)
Sometimes, it's easier to FILTER the results before applying QUERY:
=ArrayFormula(QUERY(FILTER(A1:D16, A1:A16=M3, D1:D16=N3), "SELECT Col1, Col4, SUM(Col3) GROUP BY Col1, Col4 PIVOT Col2 LABEL Col1 'Name', Col4 'Customer'",0))
As you can see, this requires using Colx notation instead of letters to indicate columns in the SELECT clause; but this is actually (in my opinion) more versatile, since you don't have to rewrite the QUERY if you ever insert columns before the existing source data.
You'll also notice that I needed to LABEL the first two columns, since FILTER will have FILTERed out the headers. (In fact, for this reason, the ranges in the formula could just as easily have begun with row 2, e.g., A2:A16, etc.)
Finally, at least in your sample spreadsheet, you didn't need the sheet name to reference the source ranges, since the result is in the same sheet.

Query parse error: CANNOT_GROUP_WITHOUT_AGG

I try to convert one table to another.
What is wrong with my syntax?
=QUERY(B21:D24, "select A,C where A is not null group by A pivot B",1)
Unable to parse query string for Function QUERY parameter 2: CANNOT_GROUP_WITHOUT_AGG
Issue:
CANNOT_GROUP_WITHOUT_AGG
means The query cannot use group columns without a AGGregate function.
select A,C where A is not null group by A pivot B
You're asking it to group by A. If the query groups by Column A, What to do with Column C? Column C cannot be displayed side by side. Although your sample doesn't have duplicates in B, think what will happen, if Column B is a,b,a instead of a,b,c. Column C needs to be aggregated. Said otherwise, For each group in A, What do you want to do with C(if there are multiple items in C for the same group in A)?
Solution:
Provide a Aggregate function for column C like, sum, count,max etc. For each group in A, The corresponding column C will be summed or counted or a max value of C in that group will be taken.
Snippet:
=QUERY(B21:D24, "select A, sum(C) where A is not null group by A pivot B",1)
Please use the following formula to get the 0's as well.
It is the IF function that will give you the 0's in your pivot.
=ArrayFormula(IF(QUERY(M1:O22, "select M, sum(O) where M is not null group by M pivot N")="",0,
QUERY(M1:O22, "select M, sum(O) where M is not null group by M pivot N")))
As for the syntax, it is mentioned that
If you use a pivot clause, then every column listed in the select clause must either be listed in the group by clause, or be wrapped by an aggregation function
Functions used:
ArrayFormula
IF
QUERY

List unique values grouped by another column

In Google Sheets, I need to extract the unique values in column B, for every unique value in column A, such that I can construct the following table:
Possible?
=Unique(A:B)
should be enough to return non-duplicate rows
https://support.google.com/docs/answer/3093198?hl=en
You can also use Sortn:
=sortn(A:B,9E+99,2,1,true,2,true)
=QUERY(QUERY(A1:B,
"select A, B, count(A)
group by A, B", 1),
"select Col1, Col2
where Col1 is not null", 1)
You can also install my add-on called Flookup and use the function below:
ULIST(colArray, [indexNum], [threshold])
colArray The range from which you want to return unique values.
indexNum The column index to analyse for unique values.
threshold The percentage similarity between the non-unique values. This helps eliminate potential duplicates.
So for your case you would simply type:
=ULIST(A1:B6, 2)
Find out more details at the official website.

joining 2 tables with specific conditions

I have 2 tables. First is original and contains row_number and several fields.
row_num col1 col2
1 a b
2 c d
3 e f
Second table is areport from first table, which shows type of mistake, col_name and row number:
m_id field row_num
m1 col1 1
m1 col1 3
m2 col2 2
And my task is to join to 2nd table the values of each field in table 1 depending on row_num and column, should be smth like:
m_id field row_num value
m1 col1 1 a
m1 col1 3 e
m2 col2 2 d
I've tryed using varios transposes like using by and id arguments, but i understand that it would be easier to use macro.
proc sort data=a1;
by row_num;
run;
proc transpose data=a1 out=a2;
by row_num;
var m_id;
run;
You could merge the (sorted) sets and use VVALUEX() to programmatically get the (character) value of a variable by name.
Something like:
data combined;
merge first second;
by row_num;
value = vvaluex(field);
keep m_id field row_num value;
run;
This assumes that row_num is unique in the first set.

Resources