Results 1 to 7 of 7
  1. #1
    slinfoot is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    3

    Multiple results returned when using SELECT TOP 1 my.Field

    Hi

    I'm using the following code to get the Mode(Most common result) from a set of values.



    Code:
    SELECT
           e.ID
         , e.ELR
         , e.Track_Id
         , e.DB_Start_Mileage
         , e.DB_End_Mileage
         , (
                    SELECT
                             TOP 1 i.[Track Cat]
                    FROM
                             qryINM2 AS i
                    WHERE
                             (
                                      i.ELR            = e.ELR
                                      AND i.[Track Id] = e.Track_Id
                                      AND
                                      (
                                               (
                                                        StartMileage   <= e.DB_Start_Mileage
                                                        AND EndMileage >= e.DB_Start_Mileage
                                               )
                                               OR
                                               (
                                                        StartMileage   <= e.DB_End_Mileage
                                                        AND EndMileage >= e.DB_End_Mileage
                                               )
                                               OR
                                               (
                                                        StartMileage   >= e.DB_Start_Mileage
                                                        AND EndMileage <= e.DB_End_Mileage
                                               )
                                      )
                             )
                    GROUP BY
                             i.[Track Cat]
                    ORDER BY
                             Count(i.[Track Cat]) DESC
           )
    FROM
           tblLNE_Eighths AS e
    Unfortunately the TOP 1 bit returns more than 1 value if there are an equal amount of Top values and the query breaks as it can only return 1 from the sub query.
    I know why this is happening but my question is what's the best way to just return the biggest value if the TOP 1 returns more that 1?
    BTW I'm using TOP 1 as the field its looking at has string values (1A, 1, 2, 3, 4, 5, 6)

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Review http://allenbrowne.com/subquery-01.html#TopN

    Need a unique identifier in the ORDER BY.
    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
    slinfoot is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    3
    Thanks interesting article. I tried this but the difference between the articles example and mine is I’m grouping first to get the Mode value, so ordering by the PK(only unique value on the table)after I’ve ordered by Count of the group only works if I group by the PK too, which gives unintended results.
    I’ve got it working in VBA using the query to populate a recordset then picking the first record. Just a bit slow that way.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    not tested but how about SELECT DISTINCT TOP 1

  5. #5
    slinfoot is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    3
    Tried it. Can’t have two aggregate functions apparently

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    Ok - how about select max rather that top 1

  7. #7
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    the problem is you group by trackCat. Suggest create another query based on this query group on
    e.ID
    , e.ELR
    , e.Track_Id
    , e.DB_Start_Mileage
    , e.DB_End_Mileage

    and select first for the Track Cat value

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

Similar Threads

  1. Replies: 3
    Last Post: 07-01-2016, 08:11 AM
  2. Replies: 3
    Last Post: 03-18-2016, 10:07 AM
  3. Replies: 1
    Last Post: 02-25-2015, 04:40 PM
  4. Replies: 10
    Last Post: 08-01-2012, 11:32 AM
  5. Returned No Results Message Box
    By Swilliams987 in forum Queries
    Replies: 2
    Last Post: 02-04-2011, 12:07 PM

Tags for this Thread

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