Results 1 to 4 of 4
  1. #1
    hdawn is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Mar 2014
    Posts
    2

    Delete based on compare

    I'm sorry if this has already been asked, but I have not found it already answered entirely yet - so I'm hoping someone can help provide some clarification.

    I have a delete query in access that I only want to delete certain rows based on if they sum to 0
    Ex:
    Tom -1
    Jill 5
    John -3
    Tom 2
    Jill -3
    John 3

    I only want to delete John's records because they sum to 0. I want to keep Tom and Jill's records.

    I have the delete where exists written -- and it is good about not deleting if none sum to 0, but if even one person sums to 0 it's trying to delete all the records. How can I get it to delete only the ones that sum to 0? Any help would be greatly appreciated.

    Here is what I have:

    DELETE *
    FROM File
    WHERE EXISTS


    (SELECT sum(File.amount) as sum, File.name
    FROM File
    GROUP BY File.name
    HAVING (((sum(File.amount))=0)))

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Have you tried building a separate subquery to manage the SUM() function? I typically don't use them but, a parameterized query may be in order here. The subquery that manages the SUM() function could be dependent on a combo in a form that would indicate the name criteria.

  3. #3
    hdawn is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Mar 2014
    Posts
    2

    Talking

    Quote Originally Posted by ItsMe View Post
    Have you tried building a separate subquery to manage the SUM() function? I typically don't use them but, a parameterized query may be in order here. The subquery that manages the SUM() function could be dependent on a combo in a form that would indicate the name criteria.

    Thanks for the reply... I may not have been doing it right, but sadly that is how I originally started. I ended up changing the where exists to a where in and removing the sum(file.amount) completely - leaving the having sum at the end and it is working. Not sure it's the best way to get to the data - but it works

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If you start to see a performance issue while running the query then consider adjusting the SQL.

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

Similar Threads

  1. Replies: 2
    Last Post: 12-27-2013, 05:20 PM
  2. Replies: 2
    Last Post: 08-10-2013, 06:06 PM
  3. Replies: 3
    Last Post: 07-14-2013, 08:24 PM
  4. Replies: 12
    Last Post: 04-25-2013, 01:32 PM
  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