2014-02-12 76 views
0

我想要至少在所有empno 101工作的相同項目上工作的empno。Oracle SQL中的所有關鍵字

我嘗試下面的查詢,但失敗:

SELECT EMPNO 
FROM EMPLOYEE 
WHERE PROJECTNO= ALL(SELECT PROJECTNO 
        FROM EMPLOYEE 
        WHERE EMPNO=101); 

EMPNO 101正在comp134和comp90 和EMPNO 103也在研究這兩個項目 但我得到的答案no rows selected爲下表。

projectno empno 
--------- ------ 
comp134  101 
comp90  101 
comp90  103  
comp14  104 
comp213  103 
comp134  103 
comp14  108 
comp90  104 
+0

Oracle和MySQL或只是其中的1? – DanFromGermany

+0

oracel sqlplus。 – ashwinbhy

+0

你希望得到什麼結果? –

回答

0

試試這個

select `projectno` from 
        (select `projectno`, count(`empno`) as counts from EMPLOYEE 
        group by `empno` 
        order by counts desc 
        limit 1)t ; 

,甚至像

select `projectno` 
from EMPLOYEE 
group by `empno` 
order by count(`empno`) desc 
limit 1 

DEMO HERE

+0

在將給我多個值,我不想要的。 如果任何員工在任何一個項目中工作,將顯示emp 101正在處理兩個項目。 我想要在emp101的所有項目中工作的員工。 – ashwinbhy

+0

@ashwinbhy嘗試我編輯的答案。 –

1

對於精確匹配簡單:

SELECT EMPNO 
FROM EMPLOYEE E1 
WHERE EXISTS 
(
SELECT 'x' FROM EMPLOYEE E2 
    WHERE E2.EMPNO=101 AND E1.PROJECTNO = E2.PROJECTNO 
) 
MINUS 
SELECT EMPNO 
FROM EMPLOYEE E1 
WHERE NOT EXISTS 
(SELECT 'x' FROM EMPLOYEE E2 
WHERE E2.EMPNO=101 AND E1.PROJECTNO = E2.PROJECTNO) 

爲atleast 這一切另外對於精確匹配

SELECT EMPNO 
FROM EMPLOYEE e 
JOIN (SELECT PROJECTNO,count(1) OVER() AS ct 
     FROM EMPLOYEE 
     WHERE EMPNO=101) my_list 
ON (e.PROJECTNO = my_list.PROJECTNO AND e.EMPNO <> 101) 
GROUP BY EMPNO 
HAVING count(*) = MAX(my_list.ct) 
0

這聽起來像是一種關係科:

with cte as 
(select PROJECTNO 
    from EMPLOYEE 
    WHERE EMPNO=101 
) 
SELECT EMPNO 
FROM EMPLOYEE as e 
join cte 
on e.PROJECTNO = cte.PROJECTNO 
group by EMPNO 
HAVING count(*) 
    = (select count(*) from cte)