Results 1 to 4 of 4
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    I need the last purchase cost


    I'm trying to find the cost of ammunition by rounds. I need the last purchase only of certain types of ammunition. However, all of the purchases appear repeating the types of ammunition bought. For instance, I have CaliberID and CaliberTypeID (Primary Keys). If I have CaliberID=1 and CaliberTypeID=1, or CaliberID=1 and CaliberTypeID=2, I don't need the older purchases of those to come up with those same primary keys. I only need the last purchase with those primary keys. With the query I wrote, all of the purchases show up. I'm not sure what I'm doing wrong.

    Click image for larger version. 

Name:	AmmoCost.jpg 
Views:	14 
Size:	94.2 KB 
ID:	47551

    Click image for larger version. 

Name:	AmmoCostResults.jpg 
Views:	14 
Size:	45.9 KB 
ID:	47552

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,952
    You want the most recent record for each CaliberID and CaliberTypeID pair?

    Review http://allenbrowne.com/subquery-01.html#TopN

    Or try:

    SELECT tblAmmoInv.* FROM tblAmmoInv INNER JOIN (SELECT CaliberID, CaliberTypeID, Max(ChangeDate) AS MaxDate FROM tblAmmoInv GROUP BY CaliberID, CaliberTypeID) AS Q ON tblAmmoInv.CaliberID = Q.CaliberID AND tblAmmoInv.CaliberTypeID = Q.CaliberTypeID AND tblAmmoInv.ChangeDate = Q.MaxDate;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Yes. But what was hanging me up was the cost. When I put the cost in, it would show all purchases. I took it out and made another query with the original table, and it worked.

    Click image for larger version. 

Name:	AmmoCostFinal.jpg 
Views:	15 
Size:	87.8 KB 
ID:	47553

  4. #4
    Join Date
    Apr 2017
    Posts
    1,687
    On fly:
    Code:
    SELECT inv.*
    FROM
    (Select invi00.CalibreID, inv00.CalibreTypeID, MAX(inv00.AmmoInvID) AS LastInvID FROM tblAmmoInv inv00 WHERE inv00.ChangeDate = (SELECT MAX(CahngeDate FROM tblAmmoInv WHERE CalibreID = inv00.CalibreID and InvTypeID = invoo.InvTypeID) GROUP BY invi00.CalibreID inv00.CalibreTypeID) inv0
    LEFT JOIN tblAmmoInv inv ON inv.AmmoInvID = inv0.LastInvID

Please reply to this thread with any new information or opinions.

Similar Threads

  1. lowest cost vendor
    By robbarkley in forum Queries
    Replies: 3
    Last Post: 09-08-2017, 01:14 AM
  2. Query to get cost
    By SunTop in forum Queries
    Replies: 1
    Last Post: 10-08-2016, 01:41 PM
  3. Resources Cost
    By salim in forum Database Design
    Replies: 2
    Last Post: 01-25-2014, 02:59 AM
  4. Most recent cost query
    By nigelbloomy in forum Queries
    Replies: 9
    Last Post: 07-12-2012, 02:41 PM
  5. Cost of a database
    By P5C768 in forum Access
    Replies: 1
    Last Post: 04-22-2010, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums