Results 1 to 4 of 4
  1. #1
    Michiel Soede is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    6

    Access Delete Query not equal to max query

    Hi,



    I have a table "TBL_DATA_RISICO_PROFIEL" of which I want to delete old records (meaning older than the most recent timestamp of every forecast in the table).
    There are 5 companies, so I have made a Max query on that same table ("TBL_DATA_RISICO_PROFIEL") that shows all the most recent time stamps per forecast per company.
    I was hoping that I could create a delete query and delete all records that not show any value if I left join these three fields. However that does not work.

    Is there anyone with a suggestion how to delete the older records with a joined table like this?

    Code until now:

    Code:
    DELETE TBL_DATA_RISICO_PROFIEL.*, TIMESTAMP_MAX.COMPANYFROM TBL_DATA_RISICO_PROFIEL LEFT JOIN TIMESTAMP_MAX ON (TBL_DATA_RISICO_PROFIEL.TIMESTAMP = TIMESTAMP_MAX.MaxOfTIMESTAMP) AND (TBL_DATA_RISICO_PROFIEL.FORECAST = TIMESTAMP_MAX.FORECAST) AND (TBL_DATA_RISICO_PROFIEL.COMPANY = TIMESTAMP_MAX.COMPANY)
    WHERE (((TIMESTAMP_MAX.COMPANY) Is Null));
    Many thanks so far,

    Michiel

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you cannot delete joined tables.
    You must use the IN keyword and a sub query.
    use the query wizard and create a FIND DUPLICATES QUERY.
    save then design the query, you'll notice the criteria in the field: IN (SELECT field in table)

    making a delete query this way WILL work. Like:
    delete * from table where [id] = ([ID] IN (select [id] from table2)

  3. #3
    Michiel Soede is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    6
    Quote Originally Posted by ranman256 View Post
    you cannot delete joined tables.
    You must use the IN keyword and a sub query.
    use the query wizard and create a FIND DUPLICATES QUERY.
    save then design the query, you'll notice the criteria in the field: IN (SELECT field in table)

    making a delete query this way WILL work. Like:
    delete * from table where [id] = ([ID] IN (select [id] from table2)
    Thanks for your reaction. I will try it tonight.

  4. #4
    Michiel Soede is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    6
    I was not able to fully reconstruct a proper query on basis of the above. Would a query like below work out? (it is not working correct yet where Access keeps on running):

    Code:
    DELETE TBL_DATA_RISICO_PROFIEL.*
    FROM TBL_DATA_RISICO_PROFIEL
    WHERE (((TBL_DATA_RISICO_PROFIEL.TIMESTAMP)<>(SELECT Max(TIMESTAMP) AS MaxTimestamp FROM TBL_DATA_RISICO_PROFIEL AS Dupe      
       WHERE (Dupe.COMPANY = TBL_DATA_RISICO_PROFIEL.COMPANY)	                      
       AND (Dupe.FORECAST = TBL_DATA_RISICO_PROFIEL.FORECAST))));

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

Similar Threads

  1. Replies: 2
    Last Post: 01-15-2014, 03:50 PM
  2. Using Is Not Equal To in a query
    By Demerit in forum Queries
    Replies: 2
    Last Post: 12-26-2013, 04:25 AM
  3. Not equal sign in query
    By j2curtis64 in forum Queries
    Replies: 3
    Last Post: 10-23-2012, 07:35 PM
  4. Delete Query Access SQL
    By tlindeman in forum Queries
    Replies: 1
    Last Post: 05-01-2012, 12:14 PM
  5. How to make my query not equal to...
    By katrinanyc926 in forum Queries
    Replies: 3
    Last Post: 12-08-2010, 11:27 AM

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