0

我在这个问题上严重超出了我的深度......我有一个带有 3 个链接表的 MS Query,它返回多个结果。我需要将它们限制为最近的条目,因为数据太大而无法按原样导入 Excel。这是当前正在使用的 SQL 的副本。我需要根据字段 inventory.lastissuedate 删除除最新条目之外的所有条目

SELECT invbalances.itemnum
     , invbalances.curbal
     , inventory.maxlevel
     , a_inventory52.minlevel
     , inventory.sstock
     , inventory.deliverytime
     , inventory.category
     , inventory.lastissuedate
FROM MX7PROD.dbo.a_inventory52 a_inventory52
    , MX7PROD.dbo.invbalances invbalances, MX7PROD.dbo.inventory inventory
WHERE invbalances.itemnum = inventory.itemnum 
AND a_inventory52.itemnum = invbalances.itemnum 
AND ((inventory.category<>'cstk' 
And inventory.category<>'ns' 
And inventory.category<>'nore' 
And inventory.category<>'sp'))
4

2 回答 2

0

尝试在最后添加:

AND inventory.lastissuedate = 
(SELECT MAX(lastissuedate) FROM MX7PROD.dbo.inventory)
于 2014-02-22T16:55:28.167 回答
0

编辑为使用表 invbalances.location 作为位置字段。

这会将您的数据限制在路边是给定项目和位置的最后一个路边的行中。

SELECT invbalances.itemnum,
       invbalances.curbal,
       inventory.maxlevel,
       a_inventory52.minlevel,
       inventory.sstock,
       inventory.deliverytime,
       inventory.category,
       inventory.lastissuedate
  FROM MX7PROD.dbo.a_inventory52 a_inventory52,
       MX7PROD.dbo.invbalances   invbalances,
       MX7PROD.dbo.inventory     inventory
 WHERE invbalances.itemnum = inventory.itemnum
   AND a_inventory52.itemnum = invbalances.itemnum
   AND inventory.category <> 'cstk'
   And inventory.category <> 'ns'
   And inventory.category <> 'nore'
   And inventory.category <> 'sp'
   and invbalances.curbal =
       (SELECT MAX(x.curbal)
          FROM MX7PROD.dbo.inventory x
         where x.itemnum = invbalances.itemnum
           and x.location = invbalances.location)
于 2014-02-22T17:50:36.393 回答