2017-08-03 155 views
1

其实我得到了我的输出,但它部分。如何使用SQL Server使用左连接显示左表中的所有记录?

在我的左表我有所有的记录,但是当我在右表中做过滤它不是从左表中把所有的记录

+0

@Realcheeselord可以将其删除你看,它只带来右表的匹配记录。它也没有显示来自左表的不匹配的记录。我想带来无与伦比的记录 –

+1

@RealCheeseLord在这种情况下,你不需要一个问题,因为你有一些叫做_Expecting output_ – Sami

+0

的可能重复[LEFT OUTER JOIN查询不返回期望的行](https://stackoverflow.com/questions/14861927/left-outer-join-query-not-returning-expected-rows) –

回答

0

我觉得左加入到家店是你是什么寻找:

from 
    ItemDynamic dynamics 
    inner join Store WITH(NOLOCK) on dynamics.StoreID = Store.ID and store.Inactive = 0 
    LEFT JOIN Item WITH(NOLOCK) on dynamics.ItemID = Item.ID and Item.ParentItem = 0 
    LEFT JOIN Sales idsp WITH(NOLOCK) on idsp.ItemID = Item.ID and Item.ParentItem = 0 and idsp.StoreID = dynamics.StoreID 
    and idsp.Time between '2017-07-16' and '2017-07-31' and idsp.StoreID <> 1001 and Item.ParentItem = 0 and Item.ItemType <> 9 
    LEFT JOIN Department WITH(NOLOCK) on Department.ID = Item.DepartmentID 
    LEFT JOIN Category WITH(NOLOCK) on Category.ID = item.CategoryID 
    LEFT JOIN Supplier WITH(NOLOCK) on Supplier.ID = item.SupplierID 
Where 
    item.ItemLookupCode = '100006C0005' and 
-- idsp.Time between '2017-07-16' and '2017-07-31' and idsp.StoreID <> 1001 and Item.ParentItem = 0 and Item.ItemType <> 9 

您可能需要添加在where子句

+0

我已经尝试过你所提到的。它不 –

+0

意味着什么之后你的输出? –

+0

仍然输出相同。 Actullay'Store和ItemDynamic'这两个表都具有所有storeID,即使我尝试使用或使用store表我可以带来结果。我尝试了与商店表的左连接和内连接。仍然输出保持不变 –

1

obviusly它doesen't显示左数据,因为在左连接状况和评论WHERE clausule排除他们

您可以将一个或idsp.Time == 0添加到您的左侧右侧使用WHERE

Where 
item.ItemLookupCode = '100006C0005' and 
(idsp.Time between '2017-07-16' and '2017-07-31' and idsp.StoreID <> 1001 and Item.ParentItem = 0 and Item.ItemType <> 9) OR idsp.Time IS NULL 
+0

时间是日期时间格式。那么怎么能给这样的'idsp.Time == 0'这样的 –

+0

值应该是null不是0抱歉,我的错误 –

1

表连接,你不应该把在WHERE子句,因为这有效将LEFT JOIN变成INNER。

移动你的条件JOIN子句本身:

LEFT JOIN Item ON dynamics.ItemID = Item.ID 
    AND item.ItemLookupCode = '100006C0005' 
    AND Item.ParentItem = 0 
    AND Item.ItemType <> 9 
LEFT JOIN Sales idsp ON idsp.ItemID = Item.ID 
    AND idsp.StoreID = dynamics.StoreID 
    AND idsp.Time between '2017-07-16' and '2017-07-31' 
    AND idsp.StoreID <> 1001 
0

添加WHERE条件您的加盟:

LEFT JOIN Sales idsp WITH(NOLOCK) 
     ON idsp.ItemID = Item.ID 
     AND Item.ParentItem = 0 
     AND idsp.StoreID = dynamics.StoreID 
     AND idsp.Time between '2017-07-16' and '2017-07-31' 
     AND idsp.StoreID <> 1001 

从你的WHERE

+0

错误的ON子句。 – jarlh

+1

很确定这是正确的 – RealCheeseLord

+1

这是正确的答案,但它是重复问题中答案的重复,只有更少的解释。 –

0
try with following code 

remove your where clause and put those conditions with your joins 




select 
    dynamics.ItemID, 
    item.ItemLookupCode, 
    dynamics.StoreID, 
    Department.Name Department, 
    Category.Name Category, 
    Supplier.Code, 
    Supplier.SupplierName, 
    sum(idsp.Qty) SoldQty, 
    sum(idsp.ExtendedCost) SoldExtCost, 
    sum(idsp.ExtendedPrice) SoldExtPrice, 
    dynamics.RestockLevel, 
    CASE WHEN isNull(sum(idsp.Qty),0) > (dynamics.RestockLevel * 0.75) THEN 'Fast Moving' 
    WHEN isNull(sum(idsp.Qty),0) > (dynamics.RestockLevel * 0.25) THEN 'Average Moving' 
    WHEN isNull(sum(idsp.Qty),0) > 0 THEN 'Slow Moving' 
    WHEN isNull(sum(idsp.Qty),0) = 0 THEN 'No Moving' END AS Moving 
from 
    ItemDynamic dynamics 
    inner join Store WITH(NOLOCK) on dynamics.StoreID = Store.ID and store.Inactive = 0 
    LEFT JOIN Item WITH(NOLOCK) on dynamics.ItemID = Item.ID and Item.ParentItem = 0 and item.ItemLookupCode = '100006C0005' 
    LEFT JOIN Sales idsp WITH(NOLOCK) on idsp.ItemID = Item.ID and Item.ParentItem = 0 and idsp.StoreID = dynamics.StoreID and idsp.Time between '2017-07-16' and '2017-07-31' and idsp.StoreID <> 1001 and Item.ParentItem = 0 and Item.ItemType <> 9 
    LEFT JOIN Department WITH(NOLOCK) on Department.ID = Item.DepartmentID 
    LEFT JOIN Category WITH(NOLOCK) on Category.ID = item.CategoryID 
    LEFT JOIN Supplier WITH(NOLOCK) on Supplier.ID = item.SupplierID 


Group By 
    dynamics.ItemID, 
    item.ItemLookupCode, 
    dynamics.StoreID, 
    dynamics.RestockLevel, 
    Department.Name, 
    Category.Name, 
    Supplier.Code, 
    Supplier.SupplierName 
order by 
    item.ItemLookupCode 
+0

我不应该在连接中过滤日期。因为用户将在任何日期进行过滤。 –

+0

你不能在连接中使用你的过滤变量吗?我想这是可能的。 –