Results 1 to 7 of 7
  1. #1
    pjdube is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    May 2017
    Posts
    25

    Query Select Distinct - Is there a nested Select Distinct?

    Dear All,



    Thanks for helping me out before. There is one thing that I am stumped on.

    The below query works great but one thing, it does not filter out the duplicates.

    Sometimes I will have TWO lessons come in from one person and I just want to show the person once, or just show the latest
    lesson from that person when it is the same day. I am thinking that somehow I will have to have a separate SELECT DISTINCT
    statement in this query.

    Here is the query I have:


    SELECT DISTINCT TM.[Full Name], TM.[Lesson In], TM.[Date In], TM.Course, TM.Section, ED.Sections, [Section]/[Sections]*100 AS [Pct Done]
    FROM TM INNER JOIN ED ON TM.Course = ED.Course
    GROUP BY TM.[Full Name], TM.[Lesson In], TM.[Date In], TM.Course, TM.Section, ED.Sections
    HAVING (((TM.[Date In])=[Forms]![Logging]![WEFilter]));

    Any pointers?

    Phil

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,896
    Same name with 2 lessons are 2 DISTINCT records so both will return since they are not duplicate records.

    If you want the latest lesson for each name for each day, that will likely involve nested TOP N. Review http://allenbrowne.com/subquery-01.html#TopN
    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
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    using distinct with group by won't do anything as in this context they are the same thing. And to show the latest you need something that will identify it - might be a time element, might be an ID, might be something else. Last means nothing without an order. If you have two records with the same date what decides which is the last one?

    Does your query actually run? you say it does but you are not grouping on your pct done calculation.

    Suggest you provide some example data and the result you want to get from that example data

  4. #4
    pjdube is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    May 2017
    Posts
    25
    Ok, to answer June 7 first,

    Yes tracking! Thanks for the link. I will look into this!

    Ajax, to answer your question, what decides which is the last one, is the Lesson Number. Here is some Example Date:

    Full Name Lesson In Date In Course Section Sections Pct Done
    Jones, John Yes 07-May-20 Blah Course 1 13 7.69230769230769
    Jones, John Yes 07-May-20 Blah Course 2 13 15.3846153846154
    Aungst, Jack Yes 07-May-20 Blah Blah Course 6 11 54.5454545454545
    Aungst, Jack Yes 07-May-20 Blah Blah Course 7 11 63.6363636363636
    Bowlings, Jill Yes 07-May-20 Blah Blah Blah Course 2 12 16.6666666666667

    From the example above you can see that I still end up with "duplicates" which is fine for another
    query I have that counts the number of lessons sent it. But this query I just want to have one instance
    of the person who sends in their lesson, preferably the latest lesson (i.e. John Jones' record being on Section 2).

    I hope this clarifies.

    Phil

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You will get unique combinations of the fields in the GROUP BY clause. Since Section is one of them, you get a record for each section. In other words, they aren't "duplicates" since they have different section values. Either drop that field from the query or in design view change it to Max, Min or whatever as appropriate to your needs.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    pjdube is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    May 2017
    Posts
    25
    Paul,

    Thank you! I knew it would be something very easy! I did as you instructed, I just changed it to Max and it works properly now.

    Thanks million.

    Phil

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help Phil!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Select Distinct
    By Ron Godbout in forum Access
    Replies: 4
    Last Post: 05-18-2016, 10:58 AM
  2. Select Distinct Help
    By fionny in forum Queries
    Replies: 2
    Last Post: 06-28-2013, 08:24 AM
  3. Select distinct
    By Paul-NYS in forum Access
    Replies: 1
    Last Post: 07-09-2012, 09:03 AM
  4. Delete and/or Select Distinct records query
    By admessing in forum Queries
    Replies: 39
    Last Post: 02-14-2012, 03:50 PM
  5. Replies: 8
    Last Post: 04-21-2011, 05:29 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