从Northwind数据库我希望获得emplyee销售所产生的总收入
员工 - >订单 - > “订单详情”
我不确定我的解决方案是否提供了正确的数据(这是部分猜测)
SELECT
Employees.FirstName, Employees.LastName,
SUM(CONVERT(MONEY, ("Order Details".UnitPrice * Quantity * (1 - Discount) / 100)) * 100) AS ExtendedPrice
FROM
((Orders
INNER JOIN
"Order Details" ON Orders.OrderID = "Order Details".OrderID)
INNER JOIN
Employees ON Orders.EmployeeID = Employees.EmployeeID)
GROUP BY
LastName, FirstName;
Northwind database structure can be found here
提前谢谢你。有一个很好的解释也很棒
答案 0 :(得分:4)
Select [fieldlist] from [table] inner join [jointable] on [join fields] group by [grouping fields]
。 fieldlist可以是计算以及要显示的实际字段名称。 “内部联接”意味着您只需要那些订单,订单详细信息,有实际匹配数据的员工 - 在您的方案中更正。 [table]和[jointable]是包含关系意义上的数据的实际表。 以下是代码的修订版本:
SELECT Employees.EmployeeID, Employees.FirstName, Employees.LastName, Sum([Order Details].UnitPrice * Quantity * (1 - Discount)) AS ExtendedPrice
FROM Orders
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
group by Employees.EmployeeID, Employees.FirstName, Employees.LastName
order by Employees.FirstName, Employees.LastName;