带有Left Join的SQL显示的值为零

时间:2015-04-01 13:22:08

标签: sql sql-server tsql

我目前有一个SQL查询设置,但我希望它忽略min_on_hand列中的0,我似乎无法弄清楚为什么这不起作用< / p>

SELECT  
    sku_master.sku, 
    sku_master.description, 
    sku_master.min_on_hand,
    sku_master.max_on_hand, 
    x.total_qty_on_hand 
FROM 
    [FCI].dbo.[sku_master] 
LEFT JOIN
    (SELECT 
         sku_master.sku, 
         sum(location_inventory.qty_on_hand) as total_qty_on_hand 
     FROM 
         [FCI].[dbo].[location_inventory] 
     JOIN 
         [FCI].dbo.[sku_master] ON location_inventory.sku = sku_master.sku
     WHERE 
         sku_master.min_on_hand > 0 
     GROUP BY 
         sku_master.sku) x ON sku_master.sku = x.sku;

4 个答案:

答案 0 :(得分:1)

正如其他人在评论中提到的那样,对子查询中的min_on_hand进行过滤无效 - 您仍然会返回sku_master中的值,但它们不会包含任何值来自x的数据。

如果您将支票移至主查询,则您将看不到min_on_hand = 0

的任何记录
SELECT  
    sku_master.sku, 
    sku_master.description, 
    sku_master.min_on_hand,
    sku_master.max_on_hand, 
    x.total_qty_on_hand 
FROM 
    [FCI].dbo.[sku_master] 
LEFT JOIN
    (SELECT 
         sku_master.sku, 
         sum(location_inventory.qty_on_hand) as total_qty_on_hand 
     FROM 
         [FCI].[dbo].[location_inventory] 
     JOIN 
         [FCI].dbo.[sku_master] ON location_inventory.sku = sku_master.sku
     GROUP BY 
         sku_master.sku) x ON sku_master.sku = x.sku
WHERE 
    sku_master.min_on_hand > 0 

答案 1 :(得分:0)

SELECT  
    sku_master.sku, 
    sku_master.description, 
    sku_master.min_on_hand,
    sku_master.max_on_hand, 
    x.total_qty_on_hand 
FROM 
    [FCI].dbo.[sku_master] 
LEFT JOIN
    (SELECT 
         sku_master.sku, 
         sum(location_inventory.qty_on_hand) as total_qty_on_hand 
     FROM 
         [FCI].[dbo].[location_inventory] 
     JOIN 
         [FCI].dbo.[sku_master] ON location_inventory.sku = sku_master.sku
     GROUP BY 
         sku_master.sku) x ON sku_master.sku = x.sku;
     WHERE 
         sku_master.min_on_hand > 0 

将WHERE移动到语句的末尾解决了问题。

答案 2 :(得分:0)

你有一个正确的答案,但为什么要加入派生表

SELECT  
    sku_master.sku, 
    sku_master.description, 
    sku_master.min_on_hand,
    sku_master.max_on_hand, 
    x.total_qty_on_hand 
 FROM [FCI].dbo.[sku_master] 
 LEFT JOIN ( SELECT sku, 
                    sum(qty_on_hand) as total_qty_on_hand 
               FROM [FCI].[dbo].[location_inventory] 
              GROUP BY sku ) x 
   ON sku_master.sku = x.sku
WHERE sku_master.min_on_hand > 0 

答案 3 :(得分:0)

我使用了这个查询。它返回库存摘要,适用于最小库存量需要超过0的所有记录。

`/ *左连接sku_master * /

    SELECT        sku_master.sku, sku_master.description, sku_master.min_on_hand, 
                  sku_master.max_on_hand, 
                  location_inventory.qty_on_hand AS total_qty_on_hand
    FROM          sku_master LEFT OUTER JOIN location_inventory 
                    ON sku_master.sku = location_inventory.sku
    GROUP BY sku_master.sku, sku_master.description, 
                  sku_master.min_on_hand, sku_master.max_on_hand, 
                  location_inventory.qty_on_hand
    HAVING        (sku_master.min_on_hand > 0)`