我应该如何编写这个SQL查询?
我有这样一个问题:我应该如何编写这个SQL查询?,sql,Sql,我有这样一个问题: 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 SELECT SUM(tab1.amount) amount FROM tab1 JOIN tab2 ON t
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
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
这个很好用。
现在我想给它添加更多的过滤器。基本上我有一个表tab4,其中有employee\u id、effective\u 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
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\u id,因此出现了一个错误
我该怎么写呢?怎么样
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英镑您是否尝试过使用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
试试这样的事情:-
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
希望这能对您有所帮助。它仍然不起作用,因为tab2是在内部查询之外声明的。你必须在内部查询中使用它。是的,这是真的。!在里面申报怎么样。