SQL 查询以查找 AX 2012 R3 中库存为零的所有 wmsLocations

A SQL query to find all wmsLocations that have zero stock in AX 2012 R3

我已经试了一天了,似乎找不到正确的方法。我需要一个 SQL 查询 returns 我所有物理库存为零的位置 ( inventSum.PostedQty + inventSum.Received - inventSum.Deducted + inventSum.Registered - inventSum.Picked <= 0) 我认为它应该像下面一样简单,但这会返回我的位置(inventSum.PostedQty + inventSum.Received - inventSum.Deducted + inventSum.Registered - inventSum.Picked > 0)。有人可以帮我弄清楚这里出了什么问题吗?

select
wmslocationid
from wmsLocation
order by wmsLocation.wMSLocationId
where
(wmsLocation.inventLocationId == inventLocationId) //default warehouse
exists join inventDim
where (inventDim.InventSiteId == inventSiteId) &&//default site
(inventDim.InventLocationId == inventLocationId) &&
(inventDim.WMSLocationId == wmsLocation.wMSLocationId)
exists join inventSum
where (inventDim.InventDimId == inventSum.InventDimId) &&
(inventSum.PostedQty + inventSum.Received - inventSum.Deducted + inventSum.Registered - inventSum.Picked <= 0);

在同一地点,有些商品的实际库存可能为零,而其他商品的实际库存可能为正,您考虑过吗?您还需要按项目过滤吗?如果您需要查找 all 商品库存为零的位置,请尝试使用 notexists 而不是 exists

暂时没地方测试,你可以试试更换

exists join inventSum
where (inventDim.InventDimId == inventSum.InventDimId) &&
(inventSum.PostedQty + inventSum.Received - inventSum.Deducted + inventSum.Registered - inventSum.Picked <= 0);

来自你对

的陈述
notexists join inventSum
where (inventDim.InventDimId == inventSum.InventDimId) &&
(inventSum.PostedQty + inventSum.Received - inventSum.Deducted + inventSum.Registered - inventSum.Picked > 0);

另外,你可以试试

select wmslocationid from inventDim
    group by wmslocationid
    where inventDim.InventSiteId        == inventSiteId         //default site
       && inventDim.InventLocationId    == inventLocationId     //default warehouse
notexists join inventSum   
    where inventSum.InventDimId == inventDim.InventDimId
       && inventSum.Closed      == NoYes::No
       && inventSum.PostedQty + inventSum.Received - inventSum.Deducted + inventSum.Registered - inventSum.Picked > 0;

我试过了,但它仍然给了我所有的记录。当我尝试 generateonly 和 forceliteral 时,看起来 sql 查询是正确的,但结果数据仍然给我不相关的值。我最终通过创建 AOT 查询对象修复了它。解决方案很长,所以我将其添加到博客 https://locus90.blogspot.co.uk/2018/05/a-sql-query-to-find-all-wmslocations.html

简而言之,我最终通过使用 wmsLocations 创建一个 AOT 查询对象来修复它,我在其中按位置 ID 分组,与 InventDim 进行内部连接,但 InventDim 具有与 InventSum 的外部连接并查看 syscomputed 的查询它的列找到了那组 wmsLocations 的物理库存,我们可以将其用作另一个查询中的范围,为我们提供所有位置。这仅适用于内部联接,因此在没有 inventdim 的情况下,每个剩余位置都需要另一个不存在的联接。

再次感谢您的帮助,如果您可以为此推荐一个简单的解决方案,请告诉我。