繁体   English   中英

在我使用SQL Server的情况下,如何使用左联接显示左表中的所有记录?

[英]How to display all record from left table using left join in my case using SQL Server?

实际上,我得到了输出,但是它是不完整的。

在我的左表中,我拥有所有记录,但是当我在右表中进行过滤时,并没有从左表中获取所有记录

显然,它不显示左数据,因为WHERE子句将其排除在外

您可以在WHERE中添加OR idsp.Time == 0

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

您不应该在LEFT JOIN右侧使用的表放在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 

我认为左联接存储是您想要的:

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子句中添加注释

将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中删除

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM