select distinct s,p,a from tablename;for each ($s,$p,$a) row returned{ select min(timestamp) as $mt from (select timestamp from tablename where p = $p and s=$s and a = $a order by timestamp desc limit 30 ) as s1; select/delete from tablename where timestamp < $mt and p = $p and s=$s and a = $a;}
select * from (select EMPLOYEE_ID, FIRST_NAME, DEPARTMENT_ID, JOB_ID, HIRE_DATE,row_number() over (partition by DEPARTMENT_ID, JOB_ID order by HIRE_DATE desc) as numerationfrom EMPLOYEESorder by DEPARTMENT_ID, JOB_ID) where numeration > 1
SELECT s, p, a, ts, rownFROM (SELECT s, p, a, ts, @rown := IF(@prevA = a AND @prevP = p AND @prevS = s, @rown + 1, 1) AS rown, @prevA := a, @prevP := p, @prevS := s FROM tablename JOIN (SELECT @prevA := NULL, @prevP := NULL, @prevS := NULL, @rown := 0) AS vars ORDER BY s, p, a, ts DESC) tempWHERE rown <= 30;
DELETE t FROM tablename t, (SELECT s, p, a, MIN(ts) AS mints FROM (SELECT s, p, a, ts, @rown := IF(@prevA = a AND @prevP = p AND @prevS = s, @rown + 1, 1) AS rown, @prevA := a, @prevP := p, @prevS := s FROM tablename JOIN (SELECT @prevA := NULL, @prevP := NULL, @prevS := NULL, @rown := 0) AS vars ORDER BY s, p, a, ts DESC) AS temp WHERE rown <= 30 GROUP BY s, p, a) lim WHERE lim.s = t.s AND lim.p = t.p AND lim.a = t.a AND lim.mints > t.ts;