如何根据另一个值的值创建列值之和的最大总和的oracle视图

时间:2015-11-30 01:07:53

标签: sql oracle view sum

我需要在Oracle 11g中创建一个可以使用这些表的视图:

员工

 FirstName | LastName | EmployeeID
-----------------------------------
    joe    |   shmo   |     1
    bob    |   moll   |     2

SALESDATA

 Employee ID | commission on sale
----------------------------------
     1       |   $20
     1       |   $30
     2       |   $50
     2       |   $60

然后总结每位员工赚取的佣金总额,并返还获得最多佣金的员工。

因此,使用示例数据时,视图将包含employee id :: 2bob moll

3 个答案:

答案 0 :(得分:0)

这可以满足您的需求

将someviewname创建为视图

Select EmployeeID, sum (commision) 
from employees
left outer join salesData on salesData.EmployeeID = employees.EmployeeID
Group by EmployeeID, commision
order by commission desc

答案 1 :(得分:0)

SELECT employeeID
FROM
  (SELECT employeeID,
    SUM(commission)
  FROM sales
  GROUP BY employeeID
  ORDER BY SUM(commission)
  )
WHERE rownum = 1

不确定为什么要对此进行查看,但希望您能解决这个问题。

答案 2 :(得分:0)

在Oracle 12g +中,您可以使用fetch

select employeeid
from sales
group by employeeid
order by sum(commission) desc
fetch first 1 row only;

在早期版本中,一种方法是使用rownum

select s.*
from (select employeeid
      from sales
      group by employeeid
      order by sum(commission) desc
     ) s
where rownum = 1;