问题描述
我需要在右表中找到最接近的值并将它们组合起来。 但是为了做到这一点,我的左连接查询在所有排列上运行,它需要大量资源来计算(我的基本表很大)
For example my A table looks like
<A,1>
<A,2>
<A,10>
And table B looks like :
<A,4>
<A,5>
<A,6>
<A,7>
For this example the result will be:
<A,1,2,10,7>
我是这么想的:
select * from (
select *,row_number() over(partition by rown order by abs(b-a) asc) diff from (
(select a,b,row_number over () rown from x) a
CROSS JOIN
(select a,b from x) b
on a.a = b.a
) )where diff =1
有没有更好更有效的方法来做到这一点?