Reputation: 2571
Actually I got my output, But it partial.
In my left table I have all the records, But when I do filter in right table it is not bringing all the records from left table
Upvotes: 1
Views: 2076
Reputation: 282
obviusly it doesen't show left data because the WHERE clausule exclude them
you can add an OR idsp.Time == 0 to the 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
Upvotes: 1
Reputation: 13969
I think left join to store is what you are looking for:
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
You might require to add the condition in left join and comment in where clause
Upvotes: 0
Reputation: 21
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
Upvotes: 0
Reputation: 18559
Table you use on the right side of LEFT JOIN you shouldn't put in WHERE clause because that effectively turns LEFT JOIN into INNER.
Move your conditions to the JOIN clause itself:
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
Upvotes: 2
Reputation: 795
Add the WHERE condition to your join:
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 remove it from your WHERE
Upvotes: 1