如何使用Sql server在我的场景中进行左连接或内连接?

时间:2017-06-20 23:52:18

标签: sql sql-server join stored-procedures left-join

我想从左表中提取所有记录,并且还想显示右表中与左表匹配的记录。

1.ItemDynamic [左表]

ItemID    StoreID    SnapShotQuantity
-------------------------------------
111       1201          50
111       1001          25
111       5000          75
111       7777          100

2.Sub Query 我的右表是子查询

这是我的子查询

SELECT 
    ViewItemMovement.ItemLookupCode,
    ViewItemMovement.StoreID,
    ViewItemMovement.ItemDescription,
    ViewItemMovement.Brand,
    Sum(ViewItemMovement.Quantity) Quantity,
    ItemDynamic.SnapShotQuantity SnapShotQuantity,
    ItemDynamic.SnapShotQuantity - Sum(ViewItemMovement.Quantity) SOH,
    Item.cost,
    (ItemDynamic.SnapShotQuantity - Sum(ViewItemMovement.Quantity)) * Item.cost AS ExtendedCost

FROM 
    FT_ViewItemMovement ViewItemMovement 
    left join Item with(NoLock) on Item.ItemLookupCode = ViewItemMovement.ItemLookupCode 
    left join ItemDynamic with(NoLock) on ItemDynamic.ItemID = Item.ID and ItemDynamic.StoreID = ViewItemMovement.StoreID

WHERE brand = 'PEPSI'
Group By  
    ViewItemMovement.ItemLookupCode,
    ViewItemMovement.StoreID,
    ViewItemMovement.ItemDescription,
    ViewItemMovement.Brand,
    Item.Cost,
    ItemDynamic.SnapShotQuantity

子查询结果如下所示

ItemID    StoreID    Quantity    SnapShotQuantity   (pls leave other columns)
-------------------------------------------------
111       1201       335            50
111       1001       223            25

3.Item [此表具有ItemLookupCode的ID,称为ItemID]

ItemLookupCode   ID
------------------------
11121111         111

我尝试了以下查询,带来来自 ItemDynamic的所有记录,其中ItemID和StoreID [左表] 匹配的记录来自子查询[右表],它显示了Quantity,SnapshotQuantity,扩展价格

select 
    id.ItemID,
    id.StoreID,
    ViewItemMovements.ItemLookupCode,
    ViewItemMovements.ItemDescription,
    ViewItemMovements.Brand,
    case when item.ItemLookupCode = item.ItemLookupCode 
        and id.StoreID = ViewItemMovements.StoreID then ViewItemMovements.Quantity 
        else 0 end Quantity,
    id.SnapShotQuantity,
    case when item.ItemLookupCode = item.ItemLookupCode 
        and id.StoreID = ViewItemMovements.StoreID then ViewItemMovements.SOH
        else id.SnapShotQuantity end SOH,

    Item.cost,
    (id.SnapShotQuantity - (case when item.ItemLookupCode = item.ItemLookupCode 
        and id.StoreID = ViewItemMovements.StoreID then ViewItemMovements.Quantity 
        else 0 end))* Item.cost AS ExtendedCost

from 
    ItemDynamic id
    left join Item with(NoLock) on Item.ID = id.ItemID 
    left join (
        SELECT 
            ViewItemMovement.ItemLookupCode,
            ViewItemMovement.StoreID,
            ViewItemMovement.ItemDescription,
            ViewItemMovement.Brand,
            Sum(ViewItemMovement.Quantity) Quantity,
            ItemDynamic.SnapShotQuantity SnapShotQuantity,
            ItemDynamic.SnapShotQuantity - Sum(ViewItemMovement.Quantity) SOH,
            Item.cost,
            (ItemDynamic.SnapShotQuantity - Sum(ViewItemMovement.Quantity)) * Item.cost AS ExtendedCost

        FROM 
            FT_ViewItemMovement ViewItemMovement 
            left join Item with(NoLock) on Item.ItemLookupCode = ViewItemMovement.ItemLookupCode 
            left join ItemDynamic with(NoLock) on ItemDynamic.ItemID = Item.ID and ItemDynamic.StoreID = ViewItemMovement.StoreID

        --WHERE brand = 'PEPSI'
        Group By  
            ViewItemMovement.ItemLookupCode,
            ViewItemMovement.StoreID,
            ViewItemMovement.ItemDescription,
            ViewItemMovement.Brand,
            Item.Cost,
            ItemDynamic.SnapShotQuantity
) ViewItemMovements     on ViewItemMovements.ItemLookupCode = Item.ItemLookupCode 
    and ViewItemMovements.StoreID = id.StoreID 
    where  ViewItemMovements.Brand = 'pepsi' and id.StoreID <> 1303
    order by ViewItemMovements.ItemLookupCode,StoreID
  

我更重视以下列,尤其是数量和SnapShotQuantity,如下所示

ItemID    StoreID    Quantity    SnapShotQuantity  (pls leave other columns) 
-------------------------------------------------
111       1201       335            50
111       1001       223            25
111       5000        0             75
111       7777        0             100
  

但是只有在运行整个查询时才会得到子查询结果。我做错了什么

1 个答案:

答案 0 :(得分:0)

由于您的LEFT JOIN条款,您的问题是INNER JOIN变得像WHERE

为了快速说明问题,我们说我有以下问题:

DECLARE @A TABLE (i INT);
INSERT @A (i) VALUES (1), (2), (3);

DECLARE @B TABLE (i INT);
INSERT @B (i) VALUES (2), (3);

SELECT *
FROM @A AS A
LEFT JOIN @B AS B ON B.i = A.i;

我会得到三行:(1, null), (2, 2), (3, 3)

但是,让我说我只想加入表B,其中i = 3.如果我改变这样的查询:

DECLARE @A TABLE (i INT);
INSERT @A (i) VALUES (1), (2), (3);

DECLARE @B TABLE (i INT);
INSERT @B (i) VALUES (2), (3);

SELECT *
FROM @A AS A
LEFT JOIN @B AS B ON B.i = A.i
WHERE B.i = 3;

我只得到一行:(3, 3)

我应该做的是让WHERE子句成为LEFT JOIN的一部分:

DECLARE @A TABLE (i INT);
INSERT @A (i) VALUES (1), (2), (3);

DECLARE @B TABLE (i INT);
INSERT @B (i) VALUES (2), (3);

SELECT *
FROM @A AS A
LEFT JOIN @B AS B ON B.i = A.i AND B.i = 3;

我的结果将如下所示:(1, null), (2, null), (3, 3) 因为它是LEFT JOIN条件的一部分,而不是最后的WHERE子句

TL; DR ,您应该更改此部分:

where ViewItemMovements.Brand = 'pepsi' and id.StoreID <> 1303

到此:

AND ViewItemMovements.Brand = 'pepsi' WHERE id.StoreID <> 1303