Count 与一个相关表一起工作得很好,但是当我添加 Sum 时,我在 Count 中得到了过度膨胀的结果。
SELECT Orders.kp_OrderID AS `Order #`,
Orders.t_ServiceLevel AS `Service Level`,
Customers.t_CustCompany AS Company,
Orders.t_JobName AS `Job Name`,
Orders.t_JobStatus AS `Status`,
Orders.d_JobDue AS Due,
Orders.ti_JobDue AS Time,
COUNT(OrderItems.kf_OrderID) AS `Line Item`,
SUM(
`OrderItemComponents`.`n_QuantityPrintMaterial` * (
`OrderItemComponents`.`n_WidthInInches` * `OrderItemComponents`.`n_HeightInInches` / 144
)
) AS SqFt,
Orders.nb_JobFinished AS Finished
FROM Orders
INNER JOIN OrderItems ON Orders.kp_OrderID = OrderItems.kf_OrderID
INNER JOIN Customers ON Orders.kf_CustomerID = Customers.kp_CustomerID
INNER JOIN OrderItemComponents ON OrderItemComponents.kf_OrderID = Orders.kp_OrderID
WHERE Orders.d_JobDue > (
DATE_SUB(CURDATE(), INTERVAL 60 DAY)
)
GROUP BY Orders.kp_OrderID
ORDER BY `Line Item` DESC