SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete records in group - but not all


Delete records in group - but not all

Author
Message
Mike Saunders NL
Mike Saunders NL
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 92
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
imex
imex
SSC Veteran
SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)

Group: General Forum Members
Points: 247 Visits: 199
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.

http://www.imoveisemexposicao.com.br
Mike Saunders NL
Mike Saunders NL
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 92
Thanks Imex, This rocks!
Mike Saunders NL
Mike Saunders NL
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 92
Hi Celko,

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

Cheers,
Mike
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98004 Visits: 38995
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Rob Taylor
Rob Taylor
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3912 Visits: 1616
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
ScottPletcher
ScottPletcher
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20314 Visits: 7427
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) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18393 Visits: 6431
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. :-P


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search