将SUM乘以另一列

时间:2014-06-29 19:02:20

标签: sql

我试图将SUM语句与另一列相乘。例如:

SELECT SUM(Orders.Sales) 

获得总销售额。

SELECT Employee.Commission_Percentage 

获得佣金百分比。

如果我想将这些结合到

SELECT SUM(Orders.Sales) * Employee.Commission_Percentage AS 'Total Income'

我必须将Employee.Commision_Percentage放在GROUP BY子句中。这会抛出计算结果。有没有其他方法可以将SUM乘以另一个列结果而不将其放入汇总或GROUP BY

2 个答案:

答案 0 :(得分:2)

如果您想要佣金总额:

SELECT SUM(o.Sales * e.Commission_Percentage) 
FROM Orders o JOIN
     Employee e
     ON o.EmployeeId = e.EmployeeId;

答案 1 :(得分:0)

使用子查询......类似于此:

select
  e.employee_id,
  s.sales * e.commission_percentage as total_income
from
  employee e
  join (
    select
      o.employee_id, sum (o.sales) as sales
    from orders.sales as o
    group by o.employee_id ) s on
  e.employee_id = s.employee_id