Results 1 to 4 of 4
  1. #1
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20

    Query out a Duplicate entry based on criteria in another field

    How could I query this so that if the Wireless Numbers are duplicated as in the picture; then look at the PersonStatus and keep only the active?


    Before someone asks, no I cannot just eliminate the Terminated unless there is a duplicate wireless number.
    Click image for larger version. 

Name:	Tele-Active.png 
Views:	7 
Size:	16.1 KB 
ID:	29736

    Thank you.

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,478
    Maybe something like this: (change the name of table and fields as needed).

    qryWirelessActive:
    SELECT tblWireless.PersonStatus, tblWireless.WirelessNumber
    FROM tblWireless
    WHERE (((tblWireless.PersonStatus)="Active"));

    qryWirelessTerminated:
    SELECT tblWireless.PersonStatus, tblWireless.WirelessNumber
    FROM tblWireless
    WHERE (((tblWireless.PersonStatus)="Terminated"));

    qryWirelessTerminatedNoActive
    SELECT qryWirelessTerminated.PersonStatus, qryWirelessTerminated.WirelessNumber
    FROM qryWirelessActive RIGHT JOIN qryWirelessTerminated ON qryWirelessActive.WirelessNumber = qryWirelessTerminated.WirelessNumber
    WHERE (((qryWirelessActive.WirelessNumber) Is Null));

    Now do a union query on qryWirelessActive and qryWirelessTerminatedNoActive

    qryWirelessUnion:
    SELECT tblWireless.PersonStatus, tblWireless.WirelessNumber
    FROM tblWireless
    WHERE (((tblWireless.PersonStatus)="Active"))
    UNION ALL SELECT qryWirelessTerminated.PersonStatus, qryWirelessTerminated.WirelessNumber
    FROM qryWirelessActive RIGHT JOIN qryWirelessTerminated ON qryWirelessActive.WirelessNumber = qryWirelessTerminated.WirelessNumber
    WHERE (((qryWirelessActive.WirelessNumber) Is Null));


    You can copy the code into the query designer in Access to see it in the grid, sometimes easier to read it that way.

  3. #3
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    Something like this would find the dupes (via the Find Duplicates Query Wizard)
    Code:
    SELECT Table2.[Wireless], Table2.[ID], Table2.[PersonStatus], Table2.[PersonW], Table2.[TotalCurrent]
    FROM Table2
    WHERE (((Table2.[Wireless]) In (SELECT [Wireless] FROM [Table2] As Tmp GROUP BY [Wireless] HAVING Count(*)>1 )))
    ORDER BY Table2.[Wireless];
    then this would delete from that query, thus deleting from your table as well
    Code:
    DELETE Table2Dupes.PersonStatus
    FROM Table2Dupes
    WHERE (((Table2Dupes.PersonStatus)<>"Active"));
    You would only have to execute the second query, as it will call the first without it being open.

    Always, and noted in my signature, test anyone's supplied code or solution on a copy of your db - especially when it involves deletion.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    It was pointed out in another post https://www.accessforums.net/showthread.php?t=67321
    where this might be done using a sub query. I thought of that, but I do struggle with those, not having wrapped my mind around their syntax. I repeat the link posted in that thread: http://allenbrowne.com/subquery-01.html and committed myself to devise a similar solution for you. Have to say it took me much longer than the first method I proposed, but I'll include it here as an alternative for you. It performed the same deletion on my test data as before, but there are no more records to deal with than what you posted. Perhaps it is not as discriminating as it seems to be. Again, the sql is not formatted for use in a form, but it should work in a query sql view as written, provided the fields a table names are a match for what you have.

    Code:
    DELETE Table2.[PersonStatus] FROM Table2 WHERE 
    (((Table2.[PersonStatus])=(SELECT PersonStatus AS Tmp FROM [Table2] As Dupe 
    WHERE (Dupe.PersonStatus <>"Active"))));
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 2
    Last Post: 05-17-2017, 06:17 AM
  2. Replies: 3
    Last Post: 05-28-2015, 10:01 AM
  3. Replies: 2
    Last Post: 10-20-2014, 03:09 PM
  4. Prevent duplicate based on two criteria
    By arothacker in forum Access
    Replies: 4
    Last Post: 02-13-2014, 06:42 AM
  5. Delete Duplicate Records Based on Criteria
    By chadd in forum Queries
    Replies: 2
    Last Post: 02-07-2012, 04:24 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