回答
我觉得左加入到家店是你是什么寻找:
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子句
我已经尝试过你所提到的。它不 –
意味着什么之后你的输出? –
仍然输出相同。 Actullay'Store和ItemDynamic'这两个表都具有所有storeID,即使我尝试使用或使用store表我可以带来结果。我尝试了与商店表的左连接和内连接。仍然输出保持不变 –
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
时间是日期时间格式。那么怎么能给这样的'idsp.Time == 0'这样的 –
值应该是null不是0抱歉,我的错误 –
表连接,你不应该把在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
添加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
我不应该在连接中过滤日期。因为用户将在任何日期进行过滤。 –
你不能在连接中使用你的过滤变量吗?我想这是可能的。 –
- 1. 双左记录SQL左连接表
- 2. sql连接,左连接。记录未显示,如果有标准的右表
- 3. mysql连接显示左表中的所有记录,并在右表中匹配记录的标记
- 4. 无法使用左外连接和在SQL Server中使用
- 5. 左外连接不返回主表中的所有记录
- 6. 左外连接在SQL Server
- 7. SQL Server光标左连接
- 8. 在三个表中使用左连接(使用连接表)
- 9. 左连接要么返回所有记录,要么没有无
- 10. SQL(MS-Access) - 左加入显示左表中的记录不在右表
- 11. 使用左连接的SQL请求
- 12. 使用左连接的SQL分组
- 13. SQL与左连接显示为0的
- 14. 左连接 - 连接两个表并从左表中拉出所有数据
- 15. MySQL加入需要显示所有记录从左表
- 16. 格式左连接mysql中选择(没有出现在左表中的所有记录)
- 17. 左连接不返回左表中的所有行
- 18. 如何在sql中使用linq中的左连接?
- 19. SQL如何更新左连接的表?
- 20. SQL Server:左连接导致行数少于左表
- 21. 使用多个左连接
- 22. MySQL左连接(使用MeekroDB)
- 23. 休眠左连接使用
- 24. 如何使用左连接linq
- 25. MySQL连接表中所有左边记录都在正确的位置
- 26. SQL查询左连接表
- 27. 选择左连接表使用条件
- 28. 如何在SQL中正确使用左连接
- 29. SQL Server的左连接问题
- 30. SQL Server的左连接3台
@Realcheeselord可以将其删除你看,它只带来右表的匹配记录。它也没有显示来自左表的不匹配的记录。我想带来无与伦比的记录 –
@RealCheeseLord在这种情况下,你不需要一个问题,因为你有一些叫做_Expecting output_ – Sami
的可能重复[LEFT OUTER JOIN查询不返回期望的行](https://stackoverflow.com/questions/14861927/left-outer-join-query-not-returning-expected-rows) –