naturalJoin
naturalJoin
joins data from a pair of tables - a left and right table - based upon one or more match columns (columnsToMatch
). The match columns establish key identifiers in the left table that will be used to find data in the right table. Any data types can be chosen as keys.
The output table contains all of the rows and columns of the left table plus additional columns containing data from the right table. For columns appended to the left table (columnsToAdd
), row values equal the row values from the right table where the key values in the left and right tables are equal. If there is no matching key in the right table, appended row values are NULL
. If there are multiple matches, the operation will fail.
Syntax
leftTable.naturalJoin(rightTable, columnsToMatch)
leftTable.naturalJoin(rightTable, columnsToMatch, columnsToAdd)
Parameters
Parameter | Type | Description |
---|---|---|
rightTable | Table | The table data is added from. |
columnsToMatch | String | Columns from the left and right tables used to join on.
|
columnsToAdd | String | The columns from the right table to add to the left table based on key.
|
Returns
A new table containing all of the rows and columns of the left table, plus additional columns containing data from the right table. For columns appended to the left table (columnsToAdd
), row values equal the row values from the right table where the key values in the left and right tables are equal. If there is no matching key in the right table, appended row values are NULL
.
Examples
In the following example, the left and right tables are joined on a matching column named DeptID
.
left = newTable(
stringCol("LastName", "Rafferty", "Jones", "Steiner", "Robins", "Smith", "Rogers", "DelaCruz"),
intCol("DeptID", 31, 33, 33, 34, 34, 36, NULL_INT),
stringCol("Telephone", "(303) 555-0162", "(303) 555-0149", "(303) 555-0184", "(303) 555-0125", "", "", "(303) 555-0160"),
)
right = newTable(
intCol("DeptID", 31, 33, 34, 35),
stringCol("DeptName", "Sales", "Engineering", "Clerical", "Marketing"),
stringCol("DeptTelephone","(303) 555-0136", "(303) 555-0162", "(303) 555-0175", "(303) 555-0171")
)
result = left.naturalJoin(right, "DeptID")
- left
- right
- result
If the right table has columns that need renaming due to an initial name match, a new column name can be supplied in the third argument of the join. In the following example, Telephone
is renamed to DeptTelephone
.
left = newTable(
stringCol("LastName", "Rafferty", "Jones", "Steiner", "Robins", "Smith", "Rogers", "DelaCruz"),
intCol("DeptID", 31, 33, 33, 34, 34, 36, NULL_INT),
stringCol("Telephone", "(303) 555-0162", "(303) 555-0149", "(303) 555-0184", "(303) 555-0125", "", "", "(303) 555-0160")
)
right = newTable(
intCol("DeptID", 31, 33, 34, 35),
stringCol("DeptName", "Sales", "Engineering", "Clerical", "Marketing"),
stringCol("Telephone","(303) 555-0136", "(303) 555-0162", "(303) 555-0175", "(303) 555-0171")
)
result = left.naturalJoin(right, "DeptID", "DeptName, DeptTelephone = Telephone")
- left
- right
- result
In some cases, the matching columns have different names in the left and right table. Below, the left table has a column named DeptNumber
that needs to be matched to the column DeptID
in the right table. To perform this match, the second argument needs the name of each column in the left and right tables.
left = newTable(
stringCol("LastName", "Rafferty", "Jones", "Steiner", "Robins", "Smith", "Rogers", "DelaCruz"),
intCol("DeptNumber", 31, 33, 33, 34, 34, 36, NULL_INT),
stringCol("Telephone", "(303) 555-0162", "(303) 555-0149", "(303) 555-0184", "(303) 555-0125", "", "", "(303) 555-0160")
)
right = newTable(
intCol("DeptID", 31, 33, 34, 35),
stringCol("DeptName", "Sales", "Engineering", "Clerical", "Marketing"),
stringCol("Telephone","(303) 555-0136", "(303) 555-0162", "(303) 555-0175", "(303) 555-0171")
)
result = left.naturalJoin(right, "DeptNumber = DeptID", "DeptName, DeptTelephone = Telephone")
- left
- right
- result