关于选择最高值的SQL查询

时间:2017-03-24 18:23:00

标签: sql

我有这两个表

create table Employee2(eid char(3), ename char(10), salary number(7,2), 
mid char(3),primary key
(eid), foreign key (mid) references Employee2); 

create table MakesD2 (aoid char(3), eid char(3), cid char(3), prices 
number, dates date, primary key
(aoid,CID), foreign key (aoid) references ArtObject2, foreign key (CID) 
references Customer2,foreign key (eid) references Employee2); 

我需要找到销售最多画作的员工。

到目前为止,我有这个。

SELECT e.ename, MAX(md.eid)
FROM Employee2 e inner join MakesD2 md on md.eid=e.eid
GROUP BY e.ename;   

1 个答案:

答案 0 :(得分:0)

WITH emp_total_sales AS 
(
    SELECT eid, sum(prices) total_price FROM MakesD2 GROUP BY eid
)
SELECT e.ename FROM Employee2 e, emp_total_sales es  
WHERE e.eid = es.eid AND es.total_price IN (SELECT MAX(total_price) FROM emp_total_sales);