1

I have a MS Access table where customer keys are matched with some interest rates of several years, one row for each year. Due to user interaction it can be the case that the exact same data of interest rates and years appears twice, but with different keys. I want to filter these in a way that

  [KEY] [year] [data1] [data2]
    1    2000    0,3     0,2
    1    2003    0,7     0,3
    1    2007    0,1     0,2
    2    2000    0,3     0,2
    2    2003    0,7     0,3
    2    2007    0,1     0,2
    2    2016    0,3     0,1

becomes

  [KEY] [year] [data1] [data2] 
    1    2000    0,3     0,2         
    1    2003    0,7     0,3    
    1    2007    0,1     0,2     
    1    2016    0,3     0,1 

so if there exists historical correspondence one of the affected keys should be deleted (and of course this key should be updated in the customer table). Does anyone have an idea how to code this?

5
  • I think you have a typo in the last row of your desired output. Should the [KEY] for 2016 be 2 ? Commented Sep 23, 2016 at 7:23
  • No, I want to delete one of the keys, so all the data should be key 1 or 2. The Background is that several rates are connected to one key, but if you update one of the rates (adding the data for 2016) and forget to agree to update the others, one extra key is produced that has the same data except the newest one. I want to write a program that reverses this process Commented Sep 23, 2016 at 7:47
  • No, I want to delete one of the keys ... which one? Commented Sep 23, 2016 at 7:50
  • In the example I just thought of the one with the lower number, but maybe it makes more sense to keep the one with the complete data Commented Sep 23, 2016 at 8:07
  • 1
    The query I gave below will retain the lower key for each given year. If you want something other than this, it probably would be possible as well. Commented Sep 23, 2016 at 8:11

1 Answer 1

2
DELETE t1.*
FROM yourTable t1
WHERE NOT EXISTS
(
    SELECT 1
    FROM 
    (
        SELECT [year], [data1], [data2], MIN([KEY]) AS KEY
        FROM yourTable
        GROUP BY [year], [data1], [data2]
    ) t2
    WHERE t1.[KEY]   = t2.[KEY]   AND
          t1.[year]  = t2.[year]  AND
          t1.[data1] = t2.[data1] AND
          t1.[data2] = t2.[data2]
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering! This works fine for the upper example, but if I add another key 3 with years e.g. 2000 and 2003 with different data1 and data2, they will be deleted. So I can't go for Min(Key) as the relationship key-year can appear more than twice. Do you have an idea how to include that? Sorry for bad elaborating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.