Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. delete old timestamped login history rows from postgresql table
  2. CREATE TABLE user_profile (
  3.   id varchar(256) PRIMARY KEY,
  4.   history_count integer
  5. )
  6.  
  7. CREATE TABLE access_history (
  8.   id varchar(256),
  9.   login_time timestamp
  10. )
  11.        
  12. SELECT access_history.id, count(*), user_profile.history_count
  13.   FROM d_access_history
  14.   LEFT JOIN d_user_profile
  15.   ON access_history.id = user_profile.id
  16.   GROUP BY access_history.id, user_profile.history_count
  17.   HAVING count(*) > user_profile.history_count;
  18.        
  19. SELECT login_time
  20.   FROM access_history
  21.   WHERE id = 'user id'
  22.   ORDER BY login_time DESC
  23.   OFFSET 200 LIMIT 1;
  24.        
  25. DELETE from access_history
  26.   WHERE id = 'user id'
  27.   AND login_time <= '2011-06-06 10:22:29.604156'
  28.        
  29. with cte
  30. as
  31. (
  32.     select id, row_nubmer() over (order by login_time desc) RowNumber
  33.     from access_history
  34. )
  35. delete cte
  36. where RowNumber > 200
  37.        
  38. delete access_history
  39. where id in
  40. (
  41.     select id
  42.     from
  43.     (
  44.         select id, row_nubmer() over (order by login_time desc) RowNumber
  45.         from access_history
  46.     ) tt
  47.     where RowNumber > 200
  48. )