加入语句在不同的行集不同的条件(Join statement with different cond

2019-10-18 13:00发布

我需要加入使用不同的行集不同条件的两个表。

例如,如果RowID < 100 on t1.ColA = t2.ColB和如果RowID >= 100 on t1.ColA = t2.ColB+1

我实现了这个如下:

... On (RowID <100 and t1.ColA=t2.ColB) OR (RowID >=100 on t1.ColA=t2.ColB+1) ...

但它是非常非常慢,所以有什么问题,什么是更好的解决方案?

Answer 1:

RowID <100 on t1.ColA=t2.ColB 

UNION ALL

If RowID >=100 on t1.ColA=t2.ColB+1

尝试这种解决方案。



Answer 2:

你可以试试这个:

On t1.ColA=CASE
WHEN RowID<100 THEN t2.ColB
WHEN RowID>=100 THEN t2.ColB+1
END

要么

SELECT
...
CASE
    WHEN RowID<100 THEN t2.Column
    WHEN RowID>=100 THEN t3.Column
END
...
Join t2 On t1.ColA=t2.ColB
Join t2 as t3 On t1.ColA=t3.ColB+1

可能是它会更快



文章来源: Join statement with different conditions on different row set