2016-09-26 109 views
-1

我正在嘗試使用三個查詢創建結果集。我有三個表格庫存表格,表格訂單表格和表格訂單明細表格。我需要能夠輸入日期範圍,並獲得訂購的表單數量,當前庫存中的數量,以及包含銷燬日期的銷售數量。最後,我要的結果集顯示:從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 
+0

加入查詢。 – Barmar

+0

@Barmar,這是我不確定如何做。 –

+2

'SELECT * FROM(QUERY1)爲Q1 JOIN(QUERY2)AS Q2 ON q1.InventoryID = q2.InventoryID JOIN(QUERY3)AS Q3導q1.InventoryID = q3.InventoryID' – Barmar

回答

1

使每個查詢成爲您加入的子查詢。

SELECT * 
FROM (
    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 
) AS q1 
LEFT JOIN (
    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 
) AS q2 ON q1.InventoryId = q2.InventoryId 
LEFT JOIN (
    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 
) AS q3 ON q1.InventoryId = q3.InventoryId 
+0

請注意,如果它們在tblinventory中退出,但不在tblformorderdetails中,則可能會遺漏一些項目。在這種情況下可能不是問題,但可能是這種技術的問題。 – Hogan

+1

@Hogan正確。在這方面你的版本可能更安全。 – Barmar

2

如果你想要一個內部聯接,你可以做到這一點

SELECT * 
FROM (...) AS Q1, (...) AS Q2, (...) AS Q3 
WHERE Q1.InventoryID = Q2.InventoryID AND Q2.InventoryID = Q3.InventoryID 

這隻會給你在所有3個查詢InventoryIDs。

你可能不希望這樣,您可能希望所有庫存的ID ...所以你做到這一點。

SELECT * 
FROM (SELECT DISTINCT InventoryID FROM tblFormOrderDetails 
     UNION ALL 
     SELECT DISTINCT InventoryID FROM tblInventory) I 
LEFT JOIN (...) Q1 ON I.InventoryID = Q1.InventoryId 
LEFT JOIN (...) Q2 ON I.InventoryID = Q2.InventoryId 
LEFT JOIN (...) Q3 ON I.InventoryID = Q3.InventoryId 
相關問題