SQL:如何显示库存中的所有项目以及在一段时间内销售的商品的总销售额?

时间:2015-11-03 21:16:39

标签: sql sql-server reporting-services

我正在尝试查找@Supplier_ID手头的所有项目,并总结自@Begin_Date以来的所有销售情况。返回的是从未销售的所有物品和自@Begin_Date以来销售的物品。在 @Begin_Date之前出售的现有物品将从结果中排除。我该如何解决这个问题?

我正在使用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]

3 个答案:

答案 0 :(得分: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

您可能希望将库存信息中的发票信息分离为子查询,并将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]

答案 1 :(得分:0)

尝试更改

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)

答案 2 :(得分:0)

问题是因为您在invoice_hdr.invoice_date子句中包含NULL的日期WHERE。只需删除它:

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