使用oracle

时间:2018-03-29 23:20:48

标签: oracle plsql

我有这张桌子" table_1"

Column
1039
3900
2345

和表_2

Column
1038
1090
3903
4502
2340
2900
4500

我需要使用表2中最接近的值更新table_1,如下所示:

1039 => 1038
3900 => 3903 

等等。

由于

1 个答案:

答案 0 :(得分:0)

交叉连接表,找到Table2中具有最小差异的列。 使用FIRST聚合函数获取此列。

SELECT a.col
    ,MIN(b.col) KEEP (
        DENSE_RANK FIRST ORDER BY (ABS(a.col - b.col))
        ) as col2
FROM Table1 a
CROSS JOIN Table2 b
GROUP BY a.col;

Demo

要更新表格,请使用上述选择中的相关更新。

update table1 o set col = ( SELECT 
    MIN(b.col) KEEP (
        DENSE_RANK FIRST ORDER BY (ABS(a.col - b.col))
        ) as col2
FROM Table1 a
CROSS JOIN Table2 b
Where a.col = o.col
GROUP BY a.col );