我有,因爲correlated subquery
糟糕表現查詢,我想取代它WIZ non-correlated subquery
。如何做到這一點:如何用非相關子查詢替換相關的子查詢?
我的查詢:
select a.emp_num, a.name , b.cont_date
from main_emp a INNER JOIN main_cont b
ON a.emp_num = b.emp_num AND a.calc_year = b.calc_year
join
(
select emp_num,calc_year, max(bb.cont_date) AS max_date from main_cont bb
GROUP BY emp_num,calc_year
) bb_max
on a.emp_num = bb_max.emp_num and a.calc_year = bb_max.calc_year and b.cont_date = bb_max.max_date
where
(0 = (select count(*) from main_serv x where x.emp_num = a.emp_num and x.calc_year = a.calc_year)
or b.cont_date > (select max(y.ser_date) from main_serv y where y.emp_num = a.emp_num and y.calc_year = a.calc_year)) -- The problem here
and a.calc_year = 2015
order by 1
現在我想這個轉換子查詢:
(0 = (select count(*) from main_serv x where x.emp_num = a.emp_num and x.calc_year = a.calc_year)
or b.cont_date > (select max(y.ser_date) from main_serv y where y.emp_num = a.emp_num and y.calc_year = a.calc_year))
要
加入這樣的:
join
(
select emp_num,calc_year, max(bb.cont_date) AS max_date from main_cont bb
GROUP BY emp_num,calc_year
) bb_max
on a.emp_num = bb_max.emp_num and a.calc_year = bb_max.calc_year and b.cont_date = bb_max.max_date
,但我不如何做到這一點,因爲我有((0 =(subquery) OR (subquery))
你可以這樣做:'INNER JOIN ( SELECT COUNT(*)AS計數,MAX(y.ser_date)AS MAXDATE 從main_serv X 其中x.emp_num = a.emp_num和x.calc_year =一個。 calc_year )tt ON tt.count = 0 AND b.cont_date
@MahmoudGamal:但是我有'OR'而不是'AND' –
和'b.cont_date>''不''' –