Results 1 to 5 of 5
  1. #1
    athyeh is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2013
    Posts
    170

    SQL If no value for current date then select next closest date

    Hi Folks,

    Was wondering if somebody could help polish this dummy query, such that if there are no records with a datestamp = user-selection, the query returns a record with the next closest date

    Code:
    SELECT DateStamp
    FROM 
    TableA  
    WHERE 
    DATESTAMP = Userselected


  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    Next closest to the user input?

    Maybe:

    SELECT Max(DateStamp) AS DS FROM TableA WHERE DateStamp<=Userselected;
    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
    athyeh is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2013
    Posts
    170
    Thank you for your help, the code below returns all records, less than the user selected datestamp (Not the max).

    Tried writing the select statement Max(Datestamp) [not Max(datestamp, datestamp)] but was getting a 'Select field not valid error' for all select fields after Datestamp

    Code:
    SELECT DISTINCT  
     MAX( Datestamp, Datestamp ) AS Datestamp,FROM 
     TableA
     RIGHT OUTER JOIN TableB
     ON 
      PID = PID
      
    WHERE 
     DATE( Datestamp, CYYMMDD ) <= &Datestamp
    UNION SELECT DISTINCT  
     MAX( Datestamp, Datestamp ) AS Datestamp,
    FROM 
     TableA
     RIGHT OUTER JOIN TableB
     ON 
      PID = PID
      
    WHERE 
     DATE( Datestamp, CYYMMDD ) <= &Datestamp

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    If you want closest, regardless of whether it's more or less, I do this type of thing (note this is SQL Server syntax, slightly different than Access):

    Code:
    strSql = "SELECT TOP 1 * FROM CabManager.dbo.Meter_Transactions M " _
           & "WHERE M.VehicleID = " & Me.cboVehicle _
           & " ORDER BY ABS(DATEDIFF(minute, M.Transaction_Timestamp, '" & Me.DateTimeField & "'))"
    The keys being the TOP predicate and ordering by the absolute difference in time.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    I do not understand

    Max(Datestamp, Datestamp)

    nor

    Date(Datestamp, CYYMMDD)

    What you show in post 3 is far different from the request in post 1.

    Does Paul's suggestion help?
    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. Find Variables closest to specified Date
    By crimedog in forum Reports
    Replies: 1
    Last Post: 01-30-2014, 11:10 AM
  2. Replies: 1
    Last Post: 01-22-2014, 03:45 PM
  3. Replies: 6
    Last Post: 12-11-2012, 09:40 PM
  4. Replies: 25
    Last Post: 11-16-2012, 12:47 PM
  5. Replies: 3
    Last Post: 10-25-2012, 10:04 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