2017-05-25 64 views
0

我的任務是顯示該經理的最低薪酬員工的MGR和薪水。最小()Access中的子查詢

我需要排除MGR未知的任何人,並且排除最低工資低於1000美元的任何羣體。結果應按工資降序排列。

下面是表:

+-------+--------+-----------+------+------------+-----------+-----------+------+ 
    | Empno | Ename | Job | Mgr | Hiredate | Sal | Comm | Dept | 
    +-------+--------+-----------+------+------------+-----------+-----------+------+ 
    | 7839 | KING | PRESIDENT |  | 11/17/1981 | $5,000.00 | $0.00  | 10 | 
    | 7782 | CLARK | MANAGER | 7839 | 6/9/1981 | $2,450.00 | $0.00  | 10 | 
    | 7934 | MILLER | CLERK  | 7782 | 1/23/1982 | $1,300.00 | $0.00  | 10 | 
    | 7902 | FORD | ANALYST | 7566 | 12/3/1981 | $3,000.00 | $0.00  | 20 | 
    | 7788 | SCOTT | ANALYST | 7566 | 12/9/1982 | $3,000.00 | $0.00  | 20 | 
    | 7876 | ADAMS | CLERK  | 7788 | 1/12/1983 | $1,100.00 | $0.00  | 20 | 
    | 7369 | SMITH | CLERK  | 7902 | 12/17/1980 | $800.00 | $0.00  | 20 | 
    | 7566 | JONES | MANAGER | 7839 | 4/2/1981 | $0.00  | $0.00  | 20 | 
    | 7698 | BLAKE | MANAGER | 7839 | 5/1/1981 | $2,850.00 | $0.00  | 30 | 
    | 7499 | ALLEN | SALESMAN | 7698 | 2/20/1981 | $1,600.00 | $300.00 | 30 | 
    | 7844 | TURNER | SALESMAN | 7698 | 9/8/1981 | $1,500.00 | $0.00  | 30 | 
    | 7521 | WARD | SALESMAN | 7698 | 2/22/1981 | $1,250.00 | $500.00 | 30 | 
    | 7654 | MARTIN | SALESMAN | 7698 | 9/28/1981 | $1,250.00 | $1,400.00 | 30 | 
    | 7900 | JAMES | CLERK  | 7698 | 12/3/1981 | $950.00 | $0.00  | 30 | 
    +-------+--------+-----------+------+------------+-----------+-----------+------+ 

這裏是我到目前爲止的代碼:

SELECT EMp.Mgr, EMp.Ename, EMp.Sal AS Sal 
FROM EMp 
GROUP BY EMp.Mgr, EMp.Ename, EMp.Sal 
HAVING (((EMp.Mgr) Is Not Null) AND ((EMp.Sal)>1000)) 
ORDER BY EMp.Sal DESC; 

我目前的代碼的問題是,它沒有考慮到最低工資的參數。我相信這需要通過使用子查詢完成,但我完全相信如何繼續...

任何人都可以請協助嗎?

+0

什麼是sal列的數據類型? –

+0

這是一個貨幣數據類型 – Darren

+0

你可以試試emp.sal> CCur(1000)? –

回答

1

試試這個:

SELECT EMp.Mgr, EMp.Ename, EMp.Sal AS Sal 
FROM EMp 
WHERE emp.Sal = (select MIN(sal) from emp as emp2 where emp2.MGr = emp.Mgr and emp2.sal > 1000) 
GROUP BY EMp.Mgr, EMp.Ename, EMp.Sal 
HAVING EMp.Mgr Is Not Null 
ORDER BY EMp.Sal DESC; 
+0

感謝Jerrad!我相信這正是我正在尋找的東西 – Darren

+0

我並不完全相信這個答案涉及到1000美元的部分。你是否只想不列出任何經理的最低薪酬僱員的薪水低於此值?此代碼將顯示所有僱員支付超過1000美元的經理的所有經理。 –

0

請嘗試

with one as 
(
SELECT EMp.Mgr,min(EMp.Sal) MinSlary 
FROM EMp 
GROUP BY EMp.Mgr 
) 
select a.Mgr,b.EName,b.Sal from one a 
inner join Emp b on a.Mgr=b.Mgr and a.MinSlary=b.Sal 
where a.Mgr is not null and a.MinSlary>1000 
+0

嗯我複製/粘貼你的代碼準確,並給出了錯誤:「無效的SQL語句;預期的'刪除','插入','程序','選擇'或'更新' – Darren

+0

訪問不支持'與'statement。 – Jerrad

+0

感謝Jerrad,對於Info,我並沒有意識到它 –

0

以下是緊密依託Jerrad的回答,但解決有關我在評論那裏提出的關切:

SELECT Emp.Mgr, Emp.Ename, Emp.Sal AS Sal 
FROM Emp 
WHERE Emp.Sal=(SELECT MIN(sal) 
       FROM Emp as Emp2 
       WHERE Emp2.MGr = Emp.Mgr 
       HAVING min(Emp2.sal) >= 1000) 
GROUP BY Emp.Mgr, Emp.Ename, Emp.Sal 
HAVING Emp.Mgr Is Not Null 
ORDER BY Emp.Sal DESC; 

使用您的樣本數據,返回與Jerrad相同的行,但省略與經理7839或7698一起。其中,這兩個管理人員的工資爲0美元和950美元。我解釋原始問題的方式(「排除最低工資低於1000美元的任何團體」),這些經理應該被排除在結果之外。