Randomly delete records based on some condition

  • Hi There,

    create table #sample

    (

    Name varchar(100),

    value int

    )

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('xxx', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('YYY', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('YYY', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('YYY', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('YYY', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('YYY', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('YYY', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    insert into #sample values ('YYY', left(RAND()*(12514/RAND() +93247)/9 *RAND(),2))

    select * from #sample

    Above is the sample records

    From that I wanted to delete some records based on following condition

    * randomly select any number of records but sum(value) = 125 and name = xxx

    Thanks in advance

  • What have you attempted thus far?

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Can't really see what you are trying to achieve. But the below will delete records based on your WHERE clause requirements

    DELETE FROM X

    FROM (

    select Value, LEAD(Value) OVER (Order By Value) AS RollingSum from #sample where [Name] = 'xxx'

    ) X

    WHERE x.RollingSum <= 125

Viewing 3 posts - 1 through 2 (of 2 total)

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