不知道我是否用规范化覆盖了它,但是有4个表:
我想要这样的东西:
( Employee_Name, Month, Total_Sales_Made_In_That_Month_In_Dollar )
我认为您可以从2张桌子中获得每个订单的总和,例如:
SELECT SUM((Order_Each_Product.Quantity) * (Product.Price))
FROM Order_Each_Product
INNER JOIN Product ON Order_Each_Product.ProductID = Product.ProductID
GROUP BY OrderID
但是您如何再次按员工将其分组并获得SUM的SUM?
答案 0 :(得分:2)
在我编写代码时,Forpas击败了我!
SELECT
Employee.Name, Year, Month, SUM(Quantity * Price) AS Total_Sales_In_Month
FROM
Order_Details
LEFT JOIN Employee
ON (Order_Details.Employee_ID = Employee.ID)
LEFT JOIN Order_Each_Product
ON (Order_Details.ID = Order_Each_Product.OrderID)
LEFT JOIN Product
ON (Order_Each_Product.ProductID = Product.ID)
GROUP BY
Employee.ID, Employee.Name, Year, Month
提示,您当前所称的Order_Details
和Order_Each_Product
通常分别称为order_headers
和order_lines
。
答案 1 :(得分:1)
按员工的ID,姓名,年和月加入所有表和组:
select
e.name,
od.Year,
od.Month,
sum(oep.quantity * p.price) Total_Sales_Made_In_That_Month_In_Dollar
from employee e
inner join order_details od on od.employee_id = e.id
inner join order_each_product oep on oep.orderid = od.id
inner join product p on p.id = oep.productid
group by e.id, e.name, od.Year, od.Month