Results 1 to 4 of 4
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281

    How to SELECT the MIN of a Batch ID for a given date

    Hello everyone ~

    I need to construct a WHERE statement that allows me to select only the records that uploaded at the earliest run time each morning.

    I currently have the following:
    Code:
    SELECT * FROM MyTable
    WHERE (dbo.MyTable.BatchID =(SELECT MAX(BatchID) FROM dbo.MyTable AS dbo.MyTable1))
    


    The BatchID's used in this table are of the format: yyyymmdd_hhmmss. Thus the first data upload for this AM was: 20190814_050127

    The schedule for the uploads has changed from once an hour to 3 times an hour.

    When the report which uses this data was run during the 5:00 AM hour the MAX(BatchID) worked just fine.

    But now the report which uses this data can be run as late a during the 7:00 AM hour - Thus, I need to target the MIN(BatchID) for the 5:00 AM hour.

    What I have tried are variations of: MIN(FORMAT; MIN(CAST; MIN(CONVERT - Etc.

    In plain English, what I'm looking for is something that will return records for current day that equals the earliest run time during the 5:00 AM hour.

    Below is where I gave up and came here with my tin cup in hand


    Code:
    SELECT * FROM MyTable
    WHERE (dbo.MyTable.BatchID =(SELECT MIN(FORMAT(GETDATE('yyyymmdd') FROM dbo.MyTable AS dbo.MyTable1))
    


    Thank You so much for any help...

  2. #2
    Join Date
    Apr 2017
    Posts
    1,679
    Something like
    Code:
    SELCT TOP 1 mt.* FROM MyTable mt WHERE mt.BatchID >= FORMAT(Date, "yyyymmdd") ORDER BY mt.BatchID ASC
    OR
    Code:
     SELCT TOP 1 mt.* FROM MyTable mt WHERE mt.BatchID >= FORMAT(Date, "yyyymmdd") & "_05000" ORDER BY mt.BatchID ASC

  3. #3
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Give a try to this query for the fisrt batch of each day:
    Code:
    SELECT CLng(Left([BatchID],8)) AS TheDay, 
    Min(CLng(Right([BatchID],6))) AS MinTime, 
    Min([BatchID]) AS FirstBatch
    FROM MyTable
    GROUP BY CLng(Left([BatchID],8));

  4. #4
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Arvil & Accesstos - Thank You ~


    The code below did the trick
    Code:
    SELECT * FROM MyTable
    WHERE MyTable.BatchID = (SELECT MIN(BatchID) FROM MyTable WHERE BatchID LIKE FORMAT(GetDate(),'yyyyMMdd') +'_05%))

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

Similar Threads

  1. Replies: 4
    Last Post: 02-25-2016, 08:18 AM
  2. Date select from form.
    By BatmanMR287 in forum Access
    Replies: 1
    Last Post: 01-21-2015, 06:18 PM
  3. Replies: 4
    Last Post: 05-28-2014, 10:09 AM
  4. Replies: 1
    Last Post: 05-09-2013, 09:13 AM
  5. Select Date Range
    By dr4ke in forum Queries
    Replies: 8
    Last Post: 06-25-2012, 07:04 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