Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

sql - What I am trying to do is, to eliminate all rows which are having modified date less than the maximum date for each entry in CI_NAME column

DELETE FROM MOBILE_CELONIS_SITES 
WHERE CI_NAME IN (SELECT CI_NAME 
                  FROM MOBILE_CELONIS_SITES 
                  GROUP BY CI_NAME 
                  HAVING COUNT(CI_NAME) > 1) 
HAVING MODIFIED_DATE != (SELECT MAX(MODIFIED_DATE) 
                         FROM MOBILE_CELONIS_SITES 
                         GROUP BY CI_NAME);

I get an error:

Execution error: Incorrect syntax near the keyword 'HAVING'.

What I am trying to do is, to eliminate all rows which are having modified date less than the maximum date for each entry in CI_NAME column

question from:https://stackoverflow.com/questions/65889880/what-i-am-trying-to-do-is-to-eliminate-all-rows-which-are-having-modified-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use exists as follows:

DELETE FROM MOBILE_CELONIS_SITES t
WHERE exists 
      (Select 1 from MOBILE_CELONIS_SITES tt
        Where t.CI_NAME = tt.CI_NAME
          And tt.MODIFIED_DATE > t.MODIFIED_DATE)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...