Results 1 to 4 of 4
  1. #1
    Patrick1977 is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    6

    Help deleting related rows based on comparing values in a count field.

    I have data like this:

    A/D Date FLD TrCnt
    A 02/24/2010 A04 1


    A 03/21/2010 H04 1
    A 07/26/2010 A04 3
    D 07/26/2010 A04 2
    A 07/07/2010 H05 2
    D 07/07/2010 H05 3
    A 09/22/2010 R05 1
    D 09/22/2010 R05 1


    A/D stands for add and delete. Essentially, I want to keep Add (A) rows only under certain situations....

    IF TrCnt for A > TrCnt for D with same Date/FLD combo, then keep the A row with that combo of Date and FLD (always delete all D rows)
    But if TrCnt for A <= that for D ... Delete the A and D records for that combo of Date and FLD

    The results for the above example would be:

    A/D Date FLD TrCnt
    A 02/24/2010 A04 1
    A 03/21/2010 H04 1
    A 07/26/2010 A04 3

    Please let me know if you have any ideas doing this using SQL or VBA code.
    Thank you!!

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    something like this looks like it will do the job

    Code:
    SELECT Adds.*
    FROM Table2 AS Adds
    WHERE Adds.AD="A" and not Exists (SELECT * FROM Table2 AS Dels WHERE AD="D" and aDate=Adds.adate and fld=adds.fld and trcnt>=adds.trcnt)
    Last edited by CJ_London; 02-14-2016 at 12:21 PM. Reason: typo!

  3. #3
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    and if you want to delete the unrequired rows then the code would be something like

    Code:
    DELETE *
    FROM Table2 AS Adds
    WHERE (AD="A" AND Exists (SELECT * FROM Table2 AS Dels WHERE AD="D" and aDate=Adds.adate and fld=adds.fld and trcnt>=adds.trcnt)) OR (AD="D")

  4. #4
    Patrick1977 is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    6
    Excellent!
    Seems so obvious after seeing your code.
    Thank you!

    Quote Originally Posted by Ajax View Post
    something like this looks like it will do the job

    Code:
    SELECT Adds.*
    FROM Table2 AS Adds
    WHERE Adds.AD="A" and not Exists (SELECT * FROM Table2 AS Dels WHERE AD="D" and aDate=Adds.adate and fld=adds.fld and trcnt>=adds.trcnt)

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

Similar Threads

  1. Replies: 3
    Last Post: 09-12-2015, 10:16 AM
  2. Replies: 3
    Last Post: 04-09-2014, 12:48 PM
  3. Replies: 1
    Last Post: 11-20-2012, 03:31 AM
  4. Replies: 2
    Last Post: 08-28-2011, 06:06 AM
  5. Count of field based on specific values
    By tazzmann67 in forum Access
    Replies: 2
    Last Post: 03-30-2011, 09:11 AM

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