Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5979617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:41:05+00:00 2026-05-22T21:41:05+00:00

I have a postgresql (v8.4) table with login id and timestamps, and I want

  • 0

I have a postgresql (v8.4) table with login id and timestamps, and I want to create a batch file that will be run periodically to remove entries when the same id has more than X number of entries in the table. Also, the number of entries to keep is stored in a separate table and can be different for each id.

The pertinent columns of the two tables:

CREATE TABLE user_profile (
  id varchar(256) PRIMARY KEY,
  history_count integer
)

CREATE TABLE access_history (
  id varchar(256),
  login_time timestamp
)

The solution I’m contemplating involves 3 SQL commands:

  1. get all login ids with excess entry counts

    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;
    
  2. loop through each entry from the above query and get the time stamp for the last entry

    SELECT login_time 
      FROM access_history 
      WHERE id = 'user id'
      ORDER BY login_time DESC 
      OFFSET 200 LIMIT 1;
    
  3. delete entries older then the time stamp retrieved from #2

    DELETE from access_history 
      WHERE id = 'user id'
      AND login_time <= '2011-06-06 10:22:29.604156'
    

I am admittedly a novice in SQL, but feel like there must a more efficient way of performing this operation.

thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:41:06+00:00Added an answer on May 22, 2026 at 9:41 pm
    with cte
    as
    (
        select id, row_number() over (order by login_time desc) RowNumber
        from access_history
    )
    delete cte
    where RowNumber > 200
    

    Replace 200 with the max number of logins you want to keep in history for each user.

    [EDIT]

    As a_horse_with_no_name mentioned, cte with delete is not supported by 8.4.
    You could use delete with a subquery like this.

    delete access_history
    where id in
    (
        select id
        from
        (
            select id, row_number() over (order by login_time desc) RowNumber
            from access_history
        ) tt
        where RowNumber > 200
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
I have created a table in postgreSQL. I want to look at the SQL
I have the following table and sequence in my postgresql-8.4 database: CREATE TABLE complexobjectpy
I have a PostgreSQL database at work that is a single table of data.
I have a PostgreSQL table with the following schema - CREATE TABLE test (
I have a PostgreSQL table that is mostly a bridge table but it also
I have the following table in PostgreSQL 8.4.5: snake=> create table gps ( id
I have one table that stores values with a point in time: CREATE TABLE
i have latitude and longitude columns in location table in PostgreSQL database, and I
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.