我有这样的查询:
SELECT SUM(tab1.amount) amount
FROM tab1
JOIN tab2
ON tab1.lp_id = tab2.id
JOIN tab3
ON tab3.employee_id = tab2.employee_id
WHERE tab1.officeid =123
AND tab3.space <> 2
这很好用。 现在我想为它添加一些过滤器。基本上我有一个表tab4,其中我有employee_id,effective_date,salary列。即对于每个员工,我们维护工资变更的日期(即每位员工多个记录)。我只想挑选最近薪水超过10000的员工。我该怎么写呢? 基本上我想要这样的东西:
SELECT SUM(tab1.amount) amount
FROM tab1
JOIN tab2
ON tab1.lp_id = tab2.id
JOIN tab3
ON tab3.employee_id = tab2.employee_id
WHERE tab1.officeid =123
AND tab3.space <> 2
AND (
select salary
from (
select *
from tab4
where tab4.employee_id = tab2.employee_id
order by effective_d desc
)
where rownum = 1
) > 10000
我正在尝试添加最后两行 - 但是我收到错误,因为我无法使用tab2.employee_id
。
我该怎么写呢?
答案 0 :(得分:0)
怎么样
SELECT t.employee_id, t.salary
FROM tab4 t
INNER JOIN (SELECT employee_id, MAX(effective_date) as eff_date
FROM tab4
GROUP BY employee_id) x
ON t.employee_id = x.employee_id AND t.effective_date = x.eff_date
WHERE t.salary > 10000
这将使您的员工薪水大于10000
答案 1 :(得分:0)
您是否尝试过使用INTERSECT
运营商?
SELECT SUM(tab1.amount) amount
FROM tab1
JOIN tab2
ON tab1.lp_id = tab2.id
JOIN tab3
ON tab3.employee_id = tab2.employee_id
WHERE tab1.officeid =123
AND tab3.space <> 2
INTERSECT(
select salary
from (
select *
from tab4, tab2
where tab4.employee_id = tab2.employee_id
order by effective_d desc
)
where rownum = 1
) > 10000
答案 2 :(得分:0)
尝试这样的事情: -
SELECT SUM(tab1.amount) amount
FROM tab1
JOIN tab2
ON tab1.lp_id = tab2.id
JOIN tab3
ON tab3.employee_id = tab2.employee_id
WHERE tab1.officeid =123
AND tab3.space <> 2
AND (
select salary
from (
select *
from tab4 join tab2
on tab4.employee_id = tab2.employee_id
order by effective_d desc
)
where rownum = 1
) > 10000
希望这可以帮到你。