google spreadsheets query sql in operator - google-sheets

First I will explain what I am trying to achieve
I have a list with several columns and rows, like this
| a | b | c
-----------------
1 | f1 | 4 | f
2 | f2 | 9 | k
3 | f3 | 1 | x
In another column outside the list I want to write a list with variable size, that contains values of column 'a'.
This list would be for a query like this (suppose that I write the variable list in the column 'd')
=QUERY(A2:D3,"SELECT a, b, c WHERE A IN D")
I think the problem is that I can not use the IN operator from sql
Is there a way to achieve this? The most important part is that I want to write manually a list with variable size for the query?

Thy this formula, in cell E1:
=FILTER(A:C,REGEXMATCH(A:A,JOIN("|",FILTER(D:D,D:D<>""))))
FILTER formula can take any array as condition parameter. REGEXMATCH + JOIN with pipe | will match true when any text from D matches text from A.
This formula is good for one more reason, you may use it as source for further query, like this: =query(my_formula, "select Col1, Col3 where Col2 > 0")

Related

use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table

I have a source table like so. As you can see, each each Lookup Value can have one or more Result.
| Lookup Value | Result |
|--------------|--------|
| a | a1 |
| a | a2 |
| a | a3 |
| b | b1 |
| b | b2 |
| c | c1 |
| c | c2 |
Then I have an input table like so:
| queries | results | | | | | |
|---------|---------|---------|---------|---------|---------|---------|
| a | ... | ... | ... | ... | ... | ... |
| c | ... | ... | ... | ... | ... | ... |
The ... for each row should be the transposed values from the lookup table. So, for example, the above table would look like this:
| queries | results | | |
|---------|---------|----|----|
| a | a1 | a2 | a3 |
| c | c1 | c2 | |
Right now I have to use multiple formulas like so:
I am trying to replace it with a single ARRAYFORMULA but it doesn't seem to work.
Is there another way to do this? Basically lookup all the matching rows from a lookup table and then transpose them?
Suppose your "Lookup Value" and "Result" data run from A1:B (with headers in A1 and B1). And suppose that your "queries" list is in D1:D (header in D1) with the "results" header in E1.
Depending on the maximum number of possible matches in B:B for any value in A:A, you could use this in E2:
=ArrayFormula(IFERROR(VLOOKUP(D2:D,QUERY(FILTER({A2:B,COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))},A2:A<>""),"Select Col1, MAX(Col2) Group By Col1 Pivot Col3"),SEQUENCE(1,10,2),0)))
If your maximum possible matches is fewer than 10 or more than 10, feel free to edit the second argument of the SEQUENCE function accordingly.
Understand that, with such an array formula that is asked to process a range, you wouldn't be able to put other data anywhere below or to the right of your "queries and results" that you've asked the array formula to assess or fill. So if you want data under it, you'll need to limit your VLOOKUP from D2:D to, say, D2:D50 (or whatever your max queries range would be). Likewise, if that second argument of the SEQUENCE function is 10, you'll have "reserved" 10 columns (i.e., E:N) for possible results, and you won't be able to put data there or you'll "break" the array formula. That being the case, you may want to give yourself some sort of visual line of demarcation around the area you've reserved for the formula's use (e.g., change the background color of the block or place a border around it, etc.).
try:
=ARRAYFORMULA(IFERROR(VLOOKUP(D:D, SPLIT(TRANSPOSE(QUERY(TRANSPOSE(IF(ISNUMBER(
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)),
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "limit 0", 1),
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)))
,,999^99)), " "), TRANSPOSE(ROW(INDIRECT("A2:A"&COUNTUNIQUE(B:B)))), 0)))

Sum of row values in Google Spreadsheets

I'm new to Google Sreadsheets syntax, so forgive me if this sounds too trivial. :)
I want to sum up the row values of certain columns in my Google Spreadsheets sheet into a new column.
I'm looking for the right command to do this.
Minimal Working Example
| Column A | Column B | Column C | Column D | Sum B + D |
|----------|----------|----------|----------|-----------|
Row 1| a1 | b1 | c1 | d1 | b1 + d1 |
Row 2| a2 | b2 | c2 | d2 | b2 + d2 |
Row 3| a3 | b3 | c3 | d3 | b3 + d3 |
I want to construct the last column (Sum B + D) with a spreadsheet formula.
In cell E1 add one of the following formulas
=B1 + D1
or
=ADD(B1,D1)
or
=SUM(B1,D1)
Note: The argument separator could be ; (semicolon) instead of , (comma). It depends on the regional setting being used.
Then fill down as necessary.
If you want to avoid to have to do fill down then use the following formula
=ArrayFormula(B1:B3 + D1:D3)
You could use the following if you wnat to sum every row in columns A, D and E
=SUM(A:A,D:D,E:E)
or this if you just want to sum, say, the first 15 rows
=SUM(A1:A15,D1:D15,E1:E15)
Basically SUM can take more than one range parameter!
It may not be what you need right now but SUMIFS are also very useful. This allows you to sum one range based on one or more conditions in other ranges.

Sort range by order of values in separate column

Given a Google Sheet with the following data:
table 1
| A | B | C
1 | q | w | e
2 | a | s | d
3 | z | x | c
table 2
| A
1 | a
2 | z
3 | q
What formula(s) can sort the rows in 'table 1' according to the stored order of values in 'table 2'?
Edit: How can this be done using a formula in only one cell?
Does this formula work as you want (assuming your tables are named ranges):
=SORT(table1,MATCH(FILTER(A:A,LEN(A:A)),table2,0),1)
See this example Sheet to see it working: https://goo.gl/veSFI4
(1) If you want to sort table 1 as a one-off, add the following in D1 and pull down, then sort on column D:-
=MATCH(A1,table2!A$1:A$3,0)
(2) If you want a table which updates dynamically, in a new sheet (say table 3) enter this in A1 and pull down:-
=table2!A1
and enter this in B1 of the new sheet and pull down and across:-
=INDEX(table1!B$1:B$3,MATCH($A1,table1!$A$1:$A$3,0))
I'm assuming there are no duplicates.

Getting the count and unique values of a column of comma separated values?

Supposing all I have is the column A below
+ +
A | B | C
+--------------|---------|----------+
| |
X, Y, Z | X | 3
| |
X, Z | Y | 2
| |
X, Y | Z | 2
+ +
How do I generate columns B and C - where the B column grabs the unique elements from A, and the C column generates a count of those values.
=ArrayFormula(QUERY(TRANSPOSE(SPLIT(JOIN(",",A:A),",")&{"";""}),"select Col1, count(Col2) group by Col1 label count(Col2) ''",0))
QUERY function
TRANSPOSE function
SPLIT function
JOIN function
Without hidden cells is possible to do it with an alternative method than the one proposed by Adam (that did not work in my case).
I have tested it with google spreadsheets (from data coming from a google form using multiple selection answers):
=UNIQUE(TRANSPOSE(SPLIT(JOIN(", ";A2:A);", ";FALSE)))
Explanation goes as follows:
JOIN to mix all the values from the A column (except A1 that could be
the header of the column, if not, substitute it by A:A) separated by
a coma
SPLIT to separate all the mixed values by their comas
TRANSPOSE to transformate the column into rows and viceversa
UNIQUE
to avoid repeated values
Take into account that my "," coma includes and space character i.e., ", " to avoid incorrect unique values because "Z" y not equal to " Z".
Is it possible to create a hidden sheet?
If yes, 1) use the SPLIT() function to separate the values into columns and 2) use COUNTIF() on the hidden sheet to get the number of values

Counting number of occurrences in column?

What would be a good approach to calculate the number of occurrences in a spreadsheet column? Can this be done with a single array formula?
Example (column A is input, columns B and C are to be auto-generated):
| A | B | C |
+-------+-------+-------+
| Name | Name | Count |
+-------+-------+-------+
| Joe | Joe | 2 |
| Lisa | Lisa | 3 |
| Jenny | Jenny | 2 |
| Lisa | | |
| Lisa | | |
| Joe | | |
| Jenny | | |
A simpler approach to this
At the beginning of column B, type
=UNIQUE(A:A)
Then in column C, use
=COUNTIF(A:A, B1)
and copy them in all row column C.
Edit: If that doesn't work for you, try using semicolon instead of comma:
=COUNTIF(A:A; B1)
Try:
=ArrayFormula(QUERY(A:A&{"",""};"select Col1, count(Col2) where Col1 != '' group by Col1 label count(Col2) 'Count'";1))
22/07/2014 Some time in the last month, Sheets has started supporting more flexible concatenation of arrays, using an embedded array. So the solution may be shortened slightly to:
=QUERY({A:A,A:A},"select Col1, count(Col2) where Col1 != '' group by Col1 label count(Col2) 'Count'",1)
=COUNTIF(A:A;"lisa")
You can replace the criteria with cell references from Column B
Just adding some extra sorting if needed
=QUERY(A2:A,"select A, count(A) where A is not null group by A order by count(A) DESC label A 'Name', count(A) 'Count'",-1)
=arrayformula(if(isblank(B2:B),iferror(1/0),mmult(sign(B2:B=TRANSPOSE(A2:A)),A2:A)))
I got this from a good tutorial - can't remember the title - probably about using MMult
Put the following in B3 (credit to #Alexander-Ivanov for the countif condition):
={UNIQUE(A3:A),ARRAYFORMULA(COUNTIF(UNIQUE(A3:A),"=" & UNIQUE(A3:A)))}
Benefits: It only requires editing 1 cell, it includes the name filtered by uniqueness, and it is concise.
Downside: it runs the unique function 3x
To use the unique function only once, split it into 2 cells:
B3: =UNIQUE(A3:A)
C3: =ARRAYFORMULA(COUNTIF(B3:B,"=" & B3:B))

Resources