Delete records in group - but not all

  • Hello all,

    I think that my question is quit simple, but hope someone can help me.

    I have a table that looks like:

    Location, bookid, bookname

    1, 100, Hello World

    1, 101, Another Book

    1, 102, Bookstores

    2, 103, Man on the Phone

    2, 104, Cup of Soup Story

    3, 105, Machine Terror

    Now I want to group by location and then delete all records in that group besides the record with the highest id in that group.

    After deletion my table has to be:

    1, 102, Bookstores

    2, 104, Cup of Soup Story

    3, 105, Machine Terror

    I canโ€™t find out how to make my DELETE queryโ€ฆplease help

    Thanks Mike

  • Hi,

    Try:

    with CTE as

    (

    select

    bookid,

    ROW_NUMBER() OVER(PARTITION BY Location ORDER BY bookid DESC) as RowNum

    from MyTable

    )

    delete from MyTable

    from MyTable as t

    join CTE as c

    on c.bookid = t.bookid and c.RowNum > 1

    Hope this helps.

  • Thanks Imex, This rocks!

  • Hi Celko,

    A lot of critics, but thanks for that! Will try to use your suggestions in my following posts.

    Cheers,

    Mike

  • CELKO (12/8/2012)


    A lot of critics, but thanks for that! Will try to use your suggestions in my following posts.

    Getting toasted by me is a ritual of passage in SQL forums ๐Ÿ˜‰

    And most of us would like to see you stop it also, just like hazing in high school and college it has no place here. It really detracts from what is normally a fairly professional site.

  • Lynn Pettis (12/8/2012)


    CELKO (12/8/2012)


    A lot of critics, but thanks for that! Will try to use your suggestions in my following posts.

    Getting toasted by me is a ritual of passage in SQL forums ๐Ÿ˜‰

    And most of us would like to see you stop it also, just like hazing in high school and college it has no place here. It really detracts from what is normally a fairly professional site.

    +1

  • I would think the old, trusty MAX() would be clearer and less overhead than the mirrored function:

    DELETE FROM tn

    FROM dbo.tablename tn

    INNER JOIN (

    SELECT location, MAX(bookid) AS bookid

    FROM dbo.tablename

    GROUP BY location

    ) AS tn_max ON

    tn.location = tn_max.location AND

    tn.bookid < tn_max.bookid

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • CELKO (12/8/2012)


    A lot of critics, but thanks for that! Will try to use your suggestions in my following posts.

    Getting toasted by me is a ritual of passage in SQL forums ๐Ÿ˜‰

    I think you must've missed me, but then again I don't post many questions. ๐Ÿ˜›


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • robert.gerald.taylor (12/9/2012)


    Lynn Pettis (12/8/2012)


    CELKO (12/8/2012)


    A lot of critics, but thanks for that! Will try to use your suggestions in my following posts.

    Getting toasted by me is a ritual of passage in SQL forums ๐Ÿ˜‰

    And most of us would like to see you stop it also, just like hazing in high school and college it has no place here. It really detracts from what is normally a fairly professional site.

    +1

    I am all for Celko's ranting.. he never takes it too far and it livens up SQL forums dramatically.

    I likely would have forgotten some of the points Celko has made if they weren't done so colorfully.

    Keep up the good work Joe!

    Now.. back to sorting that deck of punch cards..

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

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