Commisions (commisionID INT, EmployeeID, amount INT, created Datetime)
Sales (saleID INT, EmployeeID, amount INT, created datetime)
摘要表:
Employee (employeeID, totalCommisions INT, totalSales INT, created DateTime)
佣金和销售表中每位员工可以有0行或更多行。
查询#1查询是更新Employee表,将总commision加到员工的totalCommision列中,并将totalSales相同。
查询#2与上述相同,但将特定日期的总佣金 限制为100美元。因此,如果他们每天赚取150美元佣金,则将价值设置为100美元。
答案 0 :(得分:1)
试试这个:
UPDATE Employee
SET totalCommisions = Commisions.total,
totalSales = Sales.total
FROM ( SELECT employeeID, SUM(amount) AS total
FROM Commisions
GROUP BY employeeID ) AS Commisions,
( SELECT employeeID, SUM(amount) AS total
FROM Sales
GROUP BY employeeID ) AS Sales
WHERE Employee.employeeID = Commisions.employeeID
AND Employee.employeeID = Sales.employeeID
您应该自己尝试第二个查询。