Something like this is what I usually end up with
Names Default To Here(1);
dt1 = New Table("Table 1",
Add Rows(20),
New Column("x", Numeric, "Continuous", Formula(Random Normal())),
New Column("y", Numeric, "Continuous", Formula(Random Normal()))
);
dt2 = New Table("Table 2",
Add Rows(20),
New Column("x", Numeric, "Continuous", Formula(Random Normal())),
New Column("y", Numeric, "Continuous", Formula(Random Normal())),
New Column("Val", Numeric, Continuous, Formula(Random Normal(2, 100)))
);
m1 = dt1[0, {"x", "y"}];
m2 = dt2[0, {"x", "y"}];
tree = VPTree(m2);
{rows, distances} = tree << K Nearest Rows(1, m1);
dt1 << New Column("Nearest", Numeric, Ordinal, Values(rows));
dt1 << New Column("Distance", Numeric, Ordinal, Values(distances));
dt2 << New Column("Row", Numeric, Ordinal, Values(1::N Rows(dt2)));
/* Depending on the wanted result, using Update might be better */
dt_result = dt1 << Join(
With(dt2),
By Matching Columns(:Nearest = :Row),
Drop multiples(0, 0),
Include Nonmatches(0, 0),
Preserve main table order(1),
Output Table("Result")
);
/*
Close(dt1, no save);
Close(dt2, no save);
*/
Join/K Nearest Rows could be done to wrong direction as I didn't verify the results
-Jarmo