Pergunta

I am seriously out of my depth on this one... I have a MS Query with 3 linked tables returning multiple results. I need to limit them to the most recent entry because the data is too large to import into Excel as it stands. Here is a copy of the SQL that is currently being used. I need to eliminate all but the most recent entry based on the field 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'))
Foi útil?

Solução

Try adding this at the end:

AND inventory.lastissuedate = 
(SELECT MAX(lastissuedate) FROM MX7PROD.dbo.inventory)

Outras dicas

Edited to use table invbalances.location for location field.

This will limit your data to rows where the curbal is the last curbal for the given item and 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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top