Search code examples
sqlms-accessgroup-byminjet

Show other field(s) associated with record retrieved with Access query MIN() function


I am using the following query:

 SELECT [ActionItems].AutoID, Min([ActionItems].[Action Status]) AS [MinOfAction Status]
 FROM [ActionItems]
 GROUP BY [ActionItems].AutoID;

I have a custom sort set up for the field [Action Status], and this query, called "Most Urgent Action Item" is supposed to retrieve the one that is top in that sort for any given AutoID (the AutoID is identical to that in the main table ([Students]); this [ActionItems] table is secondary, if that makes sense).

Well, it works. But the [AutoID] field on its own is not helpful -- that only matches it to the record in the primary table. I also need to display the unique [ID] from the table [ActionItems] for each of these records it pulls, because [Action Status] is not unique (there are only three possible values). I tried adding the [ID] field using "Group By" or "Expression" or even "MAX" but none return the desired result:

  • "Group By" then shows ALL records for any given [AutoID], which is not helpful.
  • "Expression" returns the error

    Your query does not include the specified expression 'ID' as part of an aggregate function.

  • "MAX" will then only show one record per [AutoID] (as desired!), but the [ID] does not necessarily match up with the correct [Action Status], so it's not helpful either.

If possible, I would really prefer to show more than just the [ID] field, but I figure that is at least sufficient to then use this query as a helper for an additional, broader query.

I suspect a subquery might be useful but this is treading very unfamiliar territory... I did find some other similar questions here, but I was unable to find any that explained the answer in a way I could understand.

I hope that all makes sense; any help is greatly appreciated.


Edit: After some more searching I see that this is almost the same as this question: MS Access SQL: Aggregating on Min Value but retrieving other fields

I will continue to try to come up with a solution and will post it here if I do, though if the community feels this should be closed as a duplicate I understand.


Solution

  • Here is the code that I used to attain the desired result:

    SELECT T.AutoID, T.[Most Urgent Action Status], A.[ID]
    FROM
        (
        SELECT AutoID, Min([Action Status]) as [Most Urgent Action Status]
        from [ActionItems]
        GROUP BY AutoID
        ) as T
    INNER JOIN [ActionItems] A ON T.AutoID = A.[AutoID] AND T.[Most Urgent Action Status] = A.[Action Status]
    

    I don't know the correct terminology to describe what's happening here, but it looks to me like I am just joining a regular query to a subquery, where the subquery is basically the one I mentioned in my question.

    After confirming this displayed what I wanted, I made a second query that joined the [ActionItems] table to this query on the [AutoID] and [ID] fields; I also added some other biographical info from a join to the primary table for more context.

    Once again, this is basically a modified version of the answer from this question: MS Access SQL: Aggregating on Min Value but retrieving other fields

    Now that I understand how this works, it seems pretty intuitive. I hope this helps someone else!