我正在嘗試使用三個查詢創建結果集。我有三個表格庫存表格,表格訂單表格和表格訂單明細表格。我需要能夠輸入日期範圍,並獲得訂購的表單數量,當前庫存中的數量,以及包含銷燬日期的銷售數量。最後,我要的結果集顯示:從3個不同查詢中創建一個結果集
InventoryId, FormDescription, Product, Ordered, Shipped, Destroyed, Total ending
什麼是獲得使用這些查詢結果集的最好方法是什麼?
這是我的三個查詢
SELECT FOD.InventoryId, SUM(FOD.FormOrderAmount) as totalOrdered, FOD.FormShippedAmount FROM tblFormOrder FMO
JOIN tblFormOrderDetails FOD ON FOD.FormOrderId = FMO.FormOrderId
WHERE FMO.OrderDateTime BETWEEN '20110101' and '20120101'
AND FMO.OrderStatus IN ('S')
GROUP BY FOD.InventoryId, FOD.FormShippedAmount -- total shipped by date and inventoryid
SELECT INV.InventoryId, SUM(INV.CurrentAmount) as currentAmount, SUM(INV.OrderAmount) as OrderAmount,
(SUM(INV.OrderAmount) - SUM(INV.CurrentAmount)) as InventoryUsed
FROM tblInventory INV
where INV.CreatedOn
BETWEEN '20110101' and '20120101'
GROUP BY INV.InventoryId -- current amount based off ordered and used
select INV.InventoryId, count(*) as total
, FMO.OrderDateTime as OrderDate, Inv.FormNo, INV.FormDescription, INV.Product
from [tblinventory] INV
join tblformorderdetails FOD ON FOD.InventoryId = inv.InventoryId
join tblformorder FMO on FMO.FormOrderId = FOD.FormOrderId
where INV.DestructionDate
BETWEEN '20110101' and '20120101'
group by
FMO.OrderDateTime,
Inv.FormNo, INV.FormDescription, INV.Product, INV.InventoryId -- using count to find how many destroyed if they have a destruction date
加入查詢。 – Barmar
@Barmar,這是我不確定如何做。 –
'SELECT * FROM(QUERY1)爲Q1 JOIN(QUERY2)AS Q2 ON q1.InventoryID = q2.InventoryID JOIN(QUERY3)AS Q3導q1.InventoryID = q3.InventoryID' – Barmar