我是 SQL 的新手,我必须生成以下报告。
| Customer Name | Available Quantity | Received Quantity | Shipped Quantity
| ABC | 100 | 0 | 25 |
| XYZ | 345 | 20 | 0 |
Stock
Customer
Available Quantity
Receipt
Customer
Received Quantity
Received Date
Dispatch
Customer
Shipped Quantity
Shipped Date
股票表将显示所有客户名称。 Receipt 和 Ship 表将具有按日期计算的数量,因此 SQL 查询将具有日期参数(两个表相同)。我设法加入了一张桌子,但第三张桌子对我和日期都不适用。
SELECT dbo.Stock.Customer, SUM(dbo.Stock.[Available Quantity]) AS AvailableQty, O.ShippedQty
FROM (SELECT Customer, SUM([Shipped Quantity]) AS ShippedQty
FROM dbo.Dispatch
GROUP BY Customer) AS O LEFT OUTER JOIN
dbo.Stock ON dbo.Stock.Customer= O.Customer
GROUP BY dbo.Stock.Customer, O.ShippedQty