我试图根据每月的股票销售额选择单个员工赚取的佣金总额。 ,我有三个表(tbl_carForSale,tbl_Commision和tbl_employee)。
我可以获得员工销售的所有汽车的总价值,但我想要做的是能够计算员工可以获得的佣金总额(这是一个可变佣金)这是基于产品的价格),例如,如果产品是1000英镑,那么佣金的数额是1%(计算这是通过从产品表中获取产品的价格然后确定它在最小值和最大值之间并返回佣金。)
SELECT tbl_carForSale.name as 'employee Name', SUM(tbl_carForSale.carPrice) AS
value of cars sold'
INNER JOIN tbl_carForSale ON tbl_employee.employeeNo = tbl_carForSale.employeeNo
WHERE tbl_carForSale.soldDate BETWEEN DATEADD("m", -1 ,Date()) AND Date() AND
tbl_carForSale.bolSold = true
GROUP BY tbl_employee.name
** bolSold - 是一个真/假字段,表示汽车是否已标记为“已售出”。
我知道我需要使用嵌套的select语句,但我真的不知道如何能够总结佣金以及汽车价格?我猜我可以用两个陈述来做 - 但我根本不确定..
编辑:Com表:
ComNo minValue maxValue comAvliable
0 0 5000 2
1 5001 10000 3
2 10001 15000 4
3 15001 99999999 5
答案 0 :(得分:1)
我认为您可以加入Com表并获取您的价值观。通过使用条件连接在Com表上连接,将返回所需的comAvailable百分比。然后总结一下* carPrice:
SELECT tbl_carForSale.name as 'employee Name',
SUM(tbl_carForSale.carPrice) AS 'value of cars sold'
SUM(tbl_carForSale.carPrice * tc.comAvailable) AS 'commission on cars sold'
INNER JOIN tbl_carForSale ON
tbl_employee.employeeNo = tbl_carForSale.employeeNo
INNER JOIN tbl_Com tc ON
tbl_carForSale.carPrice >= tc.minValue
and
tbl_carForSale.carPrice <= tc.maxValue
WHERE tbl_carForSale.soldDate BETWEEN DATEADD("m", -1 ,Date()) AND Date() AND
tbl_carForSale.bolSold = true
GROUP BY tbl_employee.name
您需要注意的是Com表中的数据。如果INNER JOIN tbl_Com tc为carPrice / comAvailable映射返回多行,则会产生错误