2014-04-21 35 views
0

我沒有得到我想到的結果。 我已經嘗試使用:如何從emp表中刪除第5個最大工資,他也應該從部門表中的達拉斯

DELETE FROM emp c 
WHERE SAL IN (
    SELECT SAL 
    FROM EMP A 
    WHERE DEPTNO IN (
     SELECT DEPTNO 
     FROM DEPT 
     WHERE LOC IN ('DALLAS') 
    ) 
    AND 4 = (
     SELECT COUNT(*) 
     FROM EMP B 
     WHERE A.SAL< B.SAL 
    ) 
); 
+2

您使用了哪個數據庫? –

+0

快速澄清:達拉斯第五高的薪水,或第五高的薪水(如果它不是來自達拉斯,那麼不要刪除)? –

+0

如果這能奏效,它將會刪除薪水<=最高薪水,而不僅僅是第五名。你真正想要的是什麼? –

回答

0
DELETE FROM emp c 
WHERE SAL IN (
    SELECT TOP 1 
     sub1.SAL 
    FROM (
     SELECT TOP 5 
      SAL 
     FROM EMP A 
     inner join DEPT ON a.DEPTNO=DEPT.DEPTNO and DEPT.LOC='DALLAS' 
     ORDER BY SAL DESC 
    ) sub1 
    ORDER BY sub1.SAL ASC 
); 
0

瀏覽量超過工資必須在檢測到第五最高薪水幫助:

DELETE FROM emp where sal in(
    select sal from(
     SELECT sal, RANK() OVER (order by sal DESC) myrank 
      FROM emp) 
    where myrank=5 
) 
    and DEPTNO IN (
    SELECT DEPTNO 
    FROM DEPT 
    WHERE LOC IN ('DALLAS') 

如果你想在5日的工資是在達拉斯,即一個部門內不是跨越所有工資,請嘗試如下所示:

DELETE FROM emp where sal in(
    select sal from(
     SELECT sal, RANK() OVER (partition by depno order by sal DESC) myrank 
      FROM emp) 
    where myrank=5 
) 
    and DEPTNO IN (
    SELECT DEPTNO 
    FROM DEPT 
    WHERE LOC IN ('DALLAS') 
相關問題