
Untitled
By: a guest on
Jun 13th, 2012 | syntax:
None | size: 1.05 KB | hits: 19 | expires: Never
delete old timestamped login history rows from postgresql table
CREATE TABLE user_profile (
id varchar(256) PRIMARY KEY,
history_count integer
)
CREATE TABLE access_history (
id varchar(256),
login_time timestamp
)
SELECT access_history.id, count(*), user_profile.history_count
FROM d_access_history
LEFT JOIN d_user_profile
ON access_history.id = user_profile.id
GROUP BY access_history.id, user_profile.history_count
HAVING count(*) > user_profile.history_count;
SELECT login_time
FROM access_history
WHERE id = 'user id'
ORDER BY login_time DESC
OFFSET 200 LIMIT 1;
DELETE from access_history
WHERE id = 'user id'
AND login_time <= '2011-06-06 10:22:29.604156'
with cte
as
(
select id, row_nubmer() over (order by login_time desc) RowNumber
from access_history
)
delete cte
where RowNumber > 200
delete access_history
where id in
(
select id
from
(
select id, row_nubmer() over (order by login_time desc) RowNumber
from access_history
) tt
where RowNumber > 200
)