2015-11-03 52 views
0

我正試圖從@Supplier_ID手頭查找所有物品,並總結自@Begin_Date以來的任何銷售情況。返還的物品包括從未銷售過的所有物品以及自@Begin_Date以來出售的物品。在@Begin_Date之前出售的之前的物品不包含在結果中。我如何解決這個問題?SQL:如何顯示一段時間內出售的物品的庫存和總銷售額中的所有項目?

我正在使用SQL Server 2012和SSRS v3。

SELECT DISTINCT   
    inventory_supplier.supplier_id AS [Supp ID], 
    address.name AS Supplier, 
    inv_loc.location_id AS [Inventory Loc ID], 
    inv_mast.item_id AS [Item ID], 
    inv_mast.item_desc AS [Item Desc], 
    inv_loc.qty_on_hand AS QOH, 
    inv_loc.moving_average_cost AS MAC, 
    invoice_line.qty_shipped, 
    invoice_hdr.customer_id AS [Customer ID], 
    invoice_hdr.bill2_name AS Customer, 
    oe_line.source_loc_id AS [Sales Source Loc] 
FROM    
    inventory_supplier 
INNER JOIN 
    inv_mast ON inventory_supplier.inv_mast_uid = inv_mast.inv_mast_uid 
INNER JOIN 
    address ON inventory_supplier.supplier_id = address.id 
FULL OUTER JOIN 
    invoice_line ON inv_mast.inv_mast_uid = invoice_line.inv_mast_uid 
FULL OUTER JOIN 
    inv_loc ON inv_mast.inv_mast_uid = inv_loc.inv_mast_uid 
FULL OUTER JOIN 
    invoice_hdr ON invoice_line.invoice_no = invoice_hdr.invoice_no 
FULL OUTER JOIN 
    oe_line ON invoice_hdr.order_no = oe_line.order_no 
      AND invoice_line.inv_mast_uid = oe_line.inv_mast_uid 
WHERE   
    (inventory_supplier.supplier_id = @Supplier_ID) 
    AND (invoice_hdr.invoice_date >= @Begin_Date 
     OR invoice_hdr.invoice_date IS NULL) 
    AND (inv_loc.qty_on_hand > 0) 
ORDER BY 
    [Item ID], [Inventory Loc ID], [Customer ID], [Sales Source Loc] 

回答

1

你可以將你的invoice_hdr.invoice_date >= @Begin_Date你的連接語句

FULL OUTER JOIN 
    invoice_hdr ON invoice_line.invoice_no = invoice_hdr.invoice_no 
     AND invoice_hdr.invoice_date >= @Begin_Date 

不要看到很多FULL OUTER JOIN秒。當然你不想要LEFT JOIN在這裏?

您可能希望將庫存信息中的發票信息劃分爲子查詢,並將其左側聯接至發票信息。

SELECT DISTINCT   
    inventory_supplier.supplier_id AS [Supp ID], 
    address.name AS Supplier, 
    inv_loc.location_id AS [Inventory Loc ID], 
    inv_mast.item_id AS [Item ID], 
    inv_mast.item_desc AS [Item Desc], 
    inv_loc.qty_on_hand AS QOH, 
    inv_loc.moving_average_cost AS MAC, 
    invoices.qty_shipped, 
    invoices.customer_id AS [Customer ID], 
    invoices.bill2_name AS Customer, 
    invoices.source_loc_id AS [Sales Source Loc] 
FROM    
    inventory_supplier 
INNER JOIN 
    inv_mast ON inventory_supplier.inv_mast_uid = inv_mast.inv_mast_uid 
INNER JOIN 
    address ON inventory_supplier.supplier_id = address.id 
INNER JOIN 
    inv_loc ON inv_mast.inv_mast_uid = inv_loc.inv_mast_uid 
LEFT OUTER JOIN 
    (SELECT 
      invoice_line.inv_mast_uid, 
      invoice_line.qty_shipped, 
      invoice_hdr.customer_id, 
      invoice_hdr.bill2_name, 
      oe_line.source_loc_id 
    FROM 
      invoice_line 
      INNER JOIN 
       invoice_hdr ON invoice_line.invoice_no = invoice_hdr.invoice_no 
      INNER JOIN 
       oe_line ON invoice_hdr.order_no = oe_line.order_no 
       AND invoice_line.inv_mast_uid = oe_line.inv_mast_uid 
    WHERE 
      invoice_hdr.invoice_date >= @Begin_Date 
    ) invoices ON invoices.inv_mast_uid = inv_mast.inv_mast_uid 
WHERE   
    inventory_supplier.supplier_id = @Supplier_ID 
    AND inv_loc.qty_on_hand > 0 
ORDER BY 
    [Item ID], [Inventory Loc ID], [Customer ID], [Sales Source Loc] 
+0

感謝您的解決方案! – dmatthew

0

嘗試,因爲你是包括日期,invoice_hdr.invoice_date這是WHERE子句中NULL改變

WHERE   
(inventory_supplier.supplier_id = @Supplier_ID) 
AND (invoice_hdr.invoice_date >= @Begin_Date 
         OR invoice_hdr.invoice_date IS NULL) 
AND (inv_loc.qty_on_hand > 0) 

WHERE   
(inventory_supplier.supplier_id = @Supplier_ID) 
AND (invoice_hdr.invoice_date >= @Begin_Date) 
AND (inv_loc.qty_on_hand > 0) 
0

的問題。簡單地將其刪除:

WHERE   
(inventory_supplier.supplier_id = @Supplier_ID) 
AND (invoice_hdr.invoice_date >= @Begin_Date) 
AND (inv_loc.qty_on_hand > 0) 
相關問題