来源
ename location sal
A X 10
A x 20
B y 30
B x 40
ref_table
参考
ename location sal hike
A x '<30' 10%
B y '>30' 25%
对于Ref_table的第一条记录,我的逻辑应该是
Select Sal
from source a
left join
Ref_table B
where
a.sal<=B.sal
对于Ref_table的第二条记录,我的逻辑应该是
Select Sal
from source a
left join
Ref_table B
where
a.sal>=B.sal
答案 0 :(得分:0)
试试这个:
select Sal
from source a
left join Ref_table B on case when substring(B.Sal,1,1)='<' and a.sal<=cast(substring(B.sal,2,10) as int) then 1 when substring(B.Sal,1,1)='>' and a.sal>=cast(substring(B.sal,2,10) as int) then 1 else 0 end=1
另外要小心,因为您正在使用左连接,并且只返回符合条件的行(与内部连接类似)。
答案 1 :(得分:0)
试试这个:
select
s.sal,
s.ename
from
source s inner join
ref_table r on
s.ename = r.ename
where
(ename = 'a' and s.sal<=r.sal) or
(ename = 'b' and s.sal>=s.sal)