This question is concerning joining two databases in Google spreadsheet using =QUERY function
I have a table like so in range A1:C3
a d g
b e h
c f i
I have another table
c j m
a k n
b l o
I want the final table to look like this
a d g k n
b e h l o
c f i j m
I can do this by using a vlookup function pretty easily in cell D1 and paste it down and across, but my dataset is huge. I would need a whole page of vlookups and Google Spreadsheet tells I'm at my limit in complexities.
I look at the Google's Query Language reference... there doesn't seem to be an type of "join" functions mentioned. You would think it would be an easy "join on A" type operation.
Can anybody solves this without a vlookup?
Concatenates the elements of one or more one-dimensional arrays using a specified delimiter.
For example, let's say you have a sheet with employee data, and you want to create a new sheet that includes employee data and their manager's contact info. You can use the JOIN function to combine the data from both sheets into one master sheet. The syntax for the JOIN function is: =JOIN(range1, range2, ...)
Google QUERY Language version 0.7 (2016) doesn't include a JOIN (LEFT JOIN) operator but this could be achieved by using an array formula which result could be used as input for the QUERY function or for other uses.
Array formulas and the array handling features of Google Sheets make possible to make a JOIN between two simple tables. In order to make easier to read, the proposed formula use named ranges instead of range references.
=ArrayFormula( { table1, vlookup(ID,table2,COLUMN(Indirect("R1C2:R1C"&COLUMNS(table2),0)),0) } )
Remarks:
Indirect("R1C2:R1C"&COLUMNS(table2),0)
by an array of constants from 2 to number of columns of table2.See this sheet for an example
On 2017 Google improved the official help article in English about QUERY, QUERY function. It still doesn't include yet topics like this but could be helpful to understand how it works.
You can use ARRAYFORMULA
or YOU can just drag this formula: after an import or QUERY
-ing the first table; in the D column:
=QUERY(Sheet2!A1:C3, "Select B,C WHERE A='" & A1 & "'", 0)
So, this answers how you do it WITH a Vlookup-function, but in only one cell.
In your example, given that each table of data has the following cell references:
Table1: Sheet1!A1:C3
a d g
b e h
c f i
Table2: Sheet2!A1:C3
c j m
a k n
b l o
This is how the formula should be constructed.
=ArrayFormula(
{
Sheet1!A1:C,
vlookup(Sheet1!A1:A, {Sheet2!A1:A, Sheet2!B1:C}, {2,3}, false)
}
)
The key to get this formula to work, is to understand how to use curly brackets in the Vlookup Range. You basically define the first cell reference of the Range as the column which is to be a match to the Vlookup Search_Key. The rest of the cell references in the Range is in relation to the columns which you would like to join.
The Index is written as {2,3} to return the second and third column of the Range (the Range consists of a total of 3 columns); curly brackets has nothing to do with Arrayformula in the Vlookup Index, but is necessary to return multiple columns from the Vlookup function. The reason to not write {1,2,3} is because you would not like to include the column which is being used for the purpose of joining.
This kind of Join-formula can be utilized even if the join-column in the second table is located as the third column of that table. Let's say that the raw-data in this example would look like this:
Table1 (Sheet1):
a d g
b e h
c f i
Table2 (Sheet2):
j m c
k n a
l o b
If you write the formula like this, you'll still get the desired outcome (as displayed in the table of joined data):
=ArrayFormula(
{
Sheet1!A1:C,
vlookup(Sheet1!A1:A, {Sheet2!C1:C, Sheet2!A1:B}, {2,3}, false)
}
)
The table of joined data:
a d g k n
b e h l o
c f i j m
In the Join-formula, notice that the third column of Table2 is located as the first cell reference in the Vlookup Range!
The reason to why this works, is because when you use curly brackets in the Range (in conjunction with Arrayformula), the Vlookup Search_Key will NOT look for a column as a common denominator within the raw-data, instead it will use the Array within curly brackets as a reference to find a column as a common denominator (by default this is the first column of the Range).
I've written a comprehensive guide about this topic called:
I solved this by using Javascript LINQ (language integrated query).
It lets you specify Javascript with complex join conditions. You can also perform other SQL queries such as Grouping, Projecting, Sorting and Filtering your sheets as if they were database tables. Look at the links below.
Note that in the LINQ query language I replaced all spaces in column names with underscores to make them valid JS identifiers.
https://docs.google.com/spreadsheets/d/1DHtQlQUlo-X_YVfo-Wo-b7315sSk2pxL5ci4Y9lxvZo/edit?usp=sharing
https://script.google.com/d/1R5L2ReHJrBRwyoSoVOFLzEQZiGtxidPfPkAeVownt7SWX6TpacY7gA7j/edit?usp=sharing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With