Delele condition

  • Hi Team,

    I have a table 'library' with two columns

    user_name, access_time

    every user is having more than 500 entries, i want to keep only top 50 records for every user and delete the remaining records.

  • Minnu (8/4/2015)


    Hi Team,

    I have a table 'library' with two columns

    user_name, access_time

    every user is having more than 500 entries, i want to keep only top 50 records for every user and delete the remaining records.

    What does 'top' mean?

    You've detailed your requirement: now what is your question?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • So how do you identify the records you want to save?

    TOP 50 (with a cross apply).

    then delete the rest... so either NOT IN ... or outer join...

  • pietlinden (8/4/2015)


    So how do you identify the records you want to save?

    TOP 50 (with a cross apply).

    then delete the rest... so either NOT IN ... or outer join...

    That's not needed if you can delete directly using a CTE with ROW_NUMBER partitioned by user.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • access_time field is the accessed time of user, want to keep the recent 50 entries and delete all the remaining for every user.

  • You've been here long enough to know that you should post DDL and sample data to get a coded solution.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Minnu (8/4/2015)


    access_time field is the accessed time of user, want to keep the recent 50 entries and delete all the remaining for every user.

    with base as (

    select

    *,

    rn = row_number() over (partition by user_name order by access_time desc)

    from dbo.library

    )

    delete from base where rn > 50;

  • Hi Team,

    Below code is working.

    with base as (

    select

    *,

    rn = row_number() over (partition by user_name order by access_time desc)

    from dbo.library

    )

    delete from base where rn > 50;

    how to use the same code for DB2, because i need the same query in DB2 database also.

  • how to use the same code for DB2, because i need the same query in DB2 database also.

    I suggest you post this in a DB2 forum.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Minnu (8/5/2015)


    Hi Team,

    Below code is working.

    with base as (

    select

    *,

    rn = row_number() over (partition by user_name order by access_time desc)

    from dbo.library

    )

    delete from base where rn > 50;

    how to use the same code for DB2, because i need the same query in DB2 database also.

    IIRC, in DB2 I think you can do this: -

    DELETE FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY user_name ORDER BY access_time DESC ) FROM dbo.library) base(RN)

    WHERE RN > 50;

    But that won't work in SQL Server.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply