Results 1 to 6 of 6
  1. #1
    joey is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2014
    Posts
    7

    How do I filter a query - report to only show the records with the most recent date?

    I am having trouble getting a query or report to show only the most recent data.

    We have salesmen that use a handheld data collector scanners to count inventory in stores. The scanner data is imported to a Access table. Each record line is one scanned item. I have a query with totals that counts the records and gives me a total count of each item at the store on that date.

    I then need to filter the data to only show the most recent date. Using Max Date I get the most recent date but the count fields are showing totals for all dates. I am also getting the unique item from the earlier date in this query which I do not want.

    Here is my data table: Inventory Scans from stores.



    Scan Date Item Scanned location
    1/1/2014 item123 Store ABC
    1/1/2014 item123 Store ABC
    1/1/2014 item 456 Store ABC
    1/1/2014 item 456 Store ABC
    1/1/2014 item 456 Store ABC
    1/1/2014 item 789 Store ABC
    2/1/2014 item123 Store ABC
    2/1/2014 item123 Store ABC
    2/1/2014 item 456 Store ABC


    Here is my Query with Totals that counts the item records:

    Scan Date Item Scanned location (Item Scanned) count
    1/1/2014 item123 Store ABC 2
    1/1/2014 item 456 Store ABC 3
    1/1/2014 item 789 Store ABC 1
    2/1/2014 item123 Store ABC 2
    2/1/2014 item 456 Store ABC 1


    This is what I am trying to get - only the most recent date of counted items:
    Scan Date Item Scanned location (Item Scanned) count
    2/1/2014 item123 Store ABC 2
    2/1/2014 item 456 Store ABC 1


    This is my first post so I hope I didn't break any rules or present my question in a bad way or take up too much room. Thanks for any help.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    An aggregate (Totals) GROUP BY query might get what you want. Access Help has info on using the query builder.

    Try:

    SELECT [Item Scanned], Location, Max([Scan Date]) AS MaxDate, Count(*) AS CountItemLoc FROM tablename GROUP BY [Item Scanned], Location;
    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
    joey is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2014
    Posts
    7
    Hi,
    Thanks for the quick response!
    This returns the MAX date correctly in the date field but the item scanned (UPC) count is still totalling all records (all dates.)

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Filtering records based on value in other records of same table is tricky. If what you want to do is exclude all records that do not match the maximum date for each group, this could require subquery or domain aggregate functions.

    Review: http://allenbrowne.com/subquery-01.html#TopN
    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.

  5. #5
    joey is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2014
    Posts
    7
    Yes, I want to exclude all records that do not mathch the maximum date. The AllenBrown referral has great stuff but a bit beyond me at this point.
    Here is the SQL code that I have:

    SELECT [Master Query - Inventory Scans].CUST_LOOKUP, Max([Master Query - Inventory Scans].Scan_Date) AS MaxOfScan_Date, [Master Query - Inventory Scans].Rep_Name, [Master Query - Inventory Scans].Name, [Master Query - Inventory Scans].UPC, Count([Master Query - Inventory Scans].UPC) AS CountOfUPC
    FROM [Master Query - Inventory Scans]
    GROUP BY [Master Query - Inventory Scans].CUST_LOOKUP, [Master Query - Inventory Scans].Rep_Name, [Master Query - Inventory Scans].Name, [Master Query - Inventory Scans].UPC, [Master Query - Inventory Scans].Display_Name;

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Problem with trying the TOP N approach is the query does not have unique ID field. I presume the table does. So do the TOP N query on the table then do an aggregate query on that query.

    Otherwise try domain aggregate approach:

    SELECT * FROM [Master Query - Inventory Scans] WHERE [Scan Date]=DMax("[Scan Date]", "[Master Query - Inventory Scans]", "[Item Scanned]='" & [Item Scanned] & " AND location='" & [location] & "'")
    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.

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

Similar Threads

  1. Most recent Date query
    By SmartestIdiot in forum Queries
    Replies: 1
    Last Post: 01-11-2014, 07:56 AM
  2. Query Help Returning Most Recent Date's Value
    By cperry88 in forum Queries
    Replies: 1
    Last Post: 01-08-2014, 03:03 PM
  3. Replies: 17
    Last Post: 07-31-2013, 11:35 AM
  4. Replies: 1
    Last Post: 10-05-2012, 08:48 AM
  5. filter date from Query pass down to report
    By geraldk in forum Reports
    Replies: 1
    Last Post: 08-17-2012, 10:14 AM

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