[英]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.