﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Delete records in group - but not all / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 18:07:05 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>[quote][b]robert.gerald.taylor (12/9/2012)[/b][hr][quote][b]Lynn Pettis (12/8/2012)[/b][hr][quote][b]CELKO (12/8/2012)[/b][hr][quote] A lot of critics, but thanks for that! Will try to use your suggestions in my following posts. [/quote]Getting toasted by me is a ritual of passage in SQL forums ;-)[/quote]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.[/quote] +1[/quote]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..</description><pubDate>Tue, 11 Dec 2012 21:38:25 GMT</pubDate><dc:creator>Josh Ashwood</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>[quote][b]CELKO (12/8/2012)[/b][hr][quote] A lot of critics, but thanks for that! Will try to use your suggestions in my following posts. [/quote]Getting toasted by me is a ritual of passage in SQL forums ;-)[/quote]I think you must've missed me, but then again I don't post many questions. :-P</description><pubDate>Mon, 10 Dec 2012 17:47:08 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>I would think the old, trusty MAX() would be clearer and less overhead than the mirrored function:[code="sql"]DELETE FROM tnFROM dbo.tablename tnINNER 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 &amp;lt; tn_max.bookid[/code]</description><pubDate>Mon, 10 Dec 2012 16:12:27 GMT</pubDate><dc:creator>ScottPletcher</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>[quote][b]Lynn Pettis (12/8/2012)[/b][hr][quote][b]CELKO (12/8/2012)[/b][hr][quote] A lot of critics, but thanks for that! Will try to use your suggestions in my following posts. [/quote]Getting toasted by me is a ritual of passage in SQL forums ;-)[/quote]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.[/quote] +1</description><pubDate>Sun, 09 Dec 2012 20:54:53 GMT</pubDate><dc:creator>robert.gerald.taylor</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>[quote][b]CELKO (12/8/2012)[/b][hr][quote] A lot of critics, but thanks for that! Will try to use your suggestions in my following posts. [/quote]Getting toasted by me is a ritual of passage in SQL forums ;-)[/quote]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.</description><pubDate>Sat, 08 Dec 2012 20:43:59 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>[quote] A lot of critics, but thanks for that! Will try to use your suggestions in my following posts. [/quote]Getting toasted by me is a ritual of passage in SQL forums ;-)</description><pubDate>Sat, 08 Dec 2012 19:57:24 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>Hi Celko,A lot of critics, but thanks for that! Will try to use your suggestions in my following posts.Cheers,Mike</description><pubDate>Sat, 08 Dec 2012 17:52:24 GMT</pubDate><dc:creator>Mike Saunders NL</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules (you do not). Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. This is minimal polite behavior on SQL forums. We do not even have a table name!  Your terminology is wrong and you clearly did no research on your problem. The book industry uses a  SAN (Standard Address Number) for locations and ISBN for books identifiers. Rows are not anything like records. DELETE FROM is not a query. CREATE TABLE Library(isbn CHAR(13) NOT NULL PRIMARY KEY book_title VARCHAR (50) NOT NULL, san CHAR(10) NOT NULL);I left out the needed constraints so you can research them and learn how to do DDL correctly. I also ignored the fact that this is not normalized. [quote] Now I want to group by location and then delete all records [sic] in that group besides the record [sic] with the highest id [sic: ISBN] in that group. [/quote]DELETE FROM LibraryWHERE isbn       NOT IN       (SELECT MAX(isbn) FROM Library GROUP BY san);This smells like a homework assignment.</description><pubDate>Sat, 08 Dec 2012 16:25:02 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>Thanks Imex, This rocks!</description><pubDate>Sat, 08 Dec 2012 02:17:36 GMT</pubDate><dc:creator>Mike Saunders NL</dc:creator></item><item><title>RE: Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>Hi,Try:[code="sql"]with CTE as(    select         bookid,        ROW_NUMBER() OVER(PARTITION BY Location ORDER BY bookid DESC) as RowNum    from MyTable)delete from MyTablefrom MyTable as tjoin CTE as c    on c.bookid = t.bookid and c.RowNum &amp;gt; 1[/code]Hope this helps.</description><pubDate>Fri, 07 Dec 2012 09:43:34 GMT</pubDate><dc:creator>imex</dc:creator></item><item><title>Delete records in group - but not all</title><link>http://www.sqlservercentral.com/Forums/Topic1394128-392-1.aspx</link><description>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, bookname1, 100, Hello World1, 101, Another Book1, 102, Bookstores2, 103, Man on the Phone2, 104, Cup of Soup Story3, 105, Machine TerrorNow 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, Bookstores2, 104, Cup of Soup Story3, 105, Machine TerrorI can’t find out how to make my DELETE query…please helpThanks Mike</description><pubDate>Fri, 07 Dec 2012 09:29:02 GMT</pubDate><dc:creator>Mike Saunders NL</dc:creator></item></channel></rss>