Results 1 to 7 of 7
  1. #1
    AnneForumer is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    20

    Remove duplicates based on a priority in another column

    There are two "Bob" in the first grid. I'd like to remove one based on the values in "Hobby" column. "Tennis" has priority over "Hockey". The table in the bottom is the desired result.



    Thank you.

    Click image for larger version. 

Name:	RemoveDuplicates.png 
Views:	9 
Size:	8.5 KB 
ID:	26085

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Why does Tennis have the priority? What about Soccer or other Hobbies, where do they fall in the priority list and is it always that order?

  3. #3
    AnneForumer is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    20
    Let us make priority order as Tennis/Hockey/Soccer for this example and, yes, they are always in that order. A player can only play one game and the priority is based on costs.

    Thank you!

  4. #4
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Maybe create a new field in query to prioritize Hobby:
    HobbyPriority: IIF([Hobby] = "Tennis",1,IIF([Hobby] = "Hockey",2, IIF([Hobby] = "Soccer",3,1)))

    Then group by Name and select Max for HobbyPriority.

    You could also create a HobbyPriority table and include Hobby and Priority fields. Then link this to your example table by Hobby and do same thing in query, group by Name, and Max by Priority.

  5. #5
    AnneForumer is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    20
    Thank you!

  6. #6
    AnneForumer is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    20
    I'd like to extend this example further. There are other fields besides Name, like city and phone number which I'd like to return but only for records that were selected based on priority returned by the solution above. Any way to do that? I can't group by those fields as those fields are not part of definition of duplicates.

  7. #7
    AnneForumer is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    20
    I am trying the following but it is not quite working. It is asking me to provide the value of mt.Priority "parameter" and I don't intend it to be a parameter. Hopefully, this is just a syntax error. My intent is to include priority as a computed column in main query and join it to the subquery.

    Code:
    SELECT mt.*, IIf([HOBBY]="TENNIS",1,IIf([HOBBY]="HOCKEY",2,3)) AS Priority
    FROM MyTable AS mt
    INNER JOIN (SELECT MyTable.NAME, Min(IIf([HOBBY]="TENNIS",1,IIf([HOBBY]="HOCKEY",2,3))) AS Priority
                            FROM MyTable
                            GROUP BY MyTable.NAME
                            ) as t ON mt.NAME = t.NAME and mt.Priority = t.Priority
    Order by MyTable.NAME
    ;

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

Similar Threads

  1. Replies: 0
    Last Post: 06-17-2016, 08:56 AM
  2. Replies: 1
    Last Post: 05-14-2015, 06:38 PM
  3. Replies: 3
    Last Post: 01-30-2013, 07:44 AM
  4. Remove duplicates based on date in another column
    By mlhend2002 in forum Programming
    Replies: 5
    Last Post: 12-26-2011, 01:47 PM
  5. Remove Duplicates Based on Criteria
    By suryaprasad in forum Access
    Replies: 0
    Last Post: 04-07-2011, 10:50 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