Comment afficher tous les loggings de la table de gauche en utilisant la jointure gauche dans mon cas en utilisant SQL Server?

En fait, j'ai eu ma sortie, mais c'est partiel.

Dans ma table de gauche, j'ai tous les loggings, mais quand je filter dans la bonne table, cela ne ramène pas tous les loggings de la table de gauche

Table que vous utilisez sur le côté droit de LEFT JOIN vous ne devriez pas mettre dans la clause WHERE, car cela transforme efficacement LEFT JOIN en INNER.

Déplacez vos conditions vers la clause JOIN elle-même:

 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 

De toute évidence, il ne montre pas datatables de gauche parce que la clause WHERE les exclut

vous pouvez append un OR idsp.Time == 0 au 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 

Ajoutez la condition WHERE à votre jointure:

 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 

Et retirez-le de votre O WH

 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 

Je pense que join la boutique est ce que vous cherchez:

 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 

Vous pourriez avoir besoin d'append la condition dans la jointure gauche et de commenter dans la clause where