Results 1 to 2 of 2
  1. #1
    karambos is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Dec 2013
    Posts
    4

    How to SORT a date column and pass the thrid highest one to an SQL query

    I import a file into Access which has 2 columns:
    Serial and Date

    This file has serials from many previous dates. A Serial can appear multiple times in the serial column but only once on a particular date.

    The object of my investigation is to find those serials that appear in the last three read dates.

    Reads are done on any three days Mon-Fri. Not necessarily on consecutive days. And often not in the same week.

    I have written a VBA module that calls a query to spit out this data. Here's the SQL code for the query:



    Code:
    SELECT Serial
    FROM 3days
    WHERE date>=#12/4/2013#
    GROUP BY Serial
    HAVING count(*)=3;
    As you can see, I am currently hard coding the date. I would like to have my SQL code do this:
    SORT DISTINCT on Date descending
    Note the third date from the top as a variable
    Pass this variable into the SQL query in the where clause.

    But my SQL isn't strong enough to do this.

    I am grateful for any suggestions on how to do this.

  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,928
    Date is a reserved word and should not use reserved words as field names.

    Try:

    WHERE [date]>= (SELECT TOP 1 [date] FROM (SELECT TOP 3 DISTINCT [Date] FROM table ORDER BY [Date] DESC)) AS TopDate
    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. 3-column sort in a query
    By JeanZander in forum Queries
    Replies: 11
    Last Post: 12-28-2012, 05:50 PM
  2. Replies: 2
    Last Post: 08-12-2012, 10:56 AM
  3. Replies: 8
    Last Post: 12-21-2011, 12:50 PM
  4. Replies: 3
    Last Post: 04-19-2011, 06:41 PM
  5. Query to return only the row containing the highest date.
    By eric.opperman1@gmail.com in forum Queries
    Replies: 4
    Last Post: 03-22-2011, 08:42 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