Table 이 ..
ID, Text, Date
일케 있는데요,
각 ID 별로 .. Date 순으로 sort 해서 ..
최근 10건만 남기고 delete 를 시키고 싶은데요,,,
쿼리를 어떻게 짜야 좋을까요 ?
ㅜㅜ 가장큰 문제점 ... 위와 같이 fetch 해서 .. 10개의 row 만 구할경우 .. 각각의 ID 별 10개가 아닌 .. 전체 DB 중에서 10개만 뽑아내고 끝납니다 ㅡ.ㅜ ㅠㅠㅠㅠ (제가 잘못한걸까요 -_-aa) db2 "select id,date,account from acctmgr order by id,date desc fetch first 10 rows only" 일케 했는데 ㅡ.ㅜ
WITH TBL(ID,DATE) AS (VALUES ('1','20070101'), ('1','20070102'), ('1','20070103'), ('1','20070104'), ('1','20070105'), ('1','20070106'), ('1','20070107'), ('1','20070108'), ('1','20070109'), ('1','20070110'), ('1','20070111'), ('1','20070112'), ('1','20070113'), ('1','20070114'), ('1','20070115'), ('2','20070101'), ('2','20070102'), ('2','20070103'), ('2','20070104'), ('2','20070105'), ('2','20070106'), ('2','20070107'), ('2','20070108'), ('2','20070109'), ('2','20070110'), ('2','20070111'), ('2','20070112'), ('2','20070113'), ('2','20070114'), ('3','20070101'), ('3','20070102'), ('3','20070103'), ('3','20070104'), ('3','20070105'), ('3','20070106'), ('3','20070107'), ('3','20070108'), ('3','20070109'), ('3','20070110'), ('3','20070111'), ('3','20070112'), ('3','20070113')) -- SELECT * FROM TBL T1 WHERE (SELECT COUNT(*) FROM TBL T2 WHERE T2.ID = T1.ID AND T2.DATE > T1.DATE ) >= 10 -- SELECT 대신 DELETE 사용해 보세요, WHERE 절은 위와 같이하시고요