Solved

How to locate nth item in an MSSQL table

Posted on 2009-05-11
8
213 Views
Last Modified: 2012-06-27
I have a log table. Once table has > 10000 row in the table I need to remove earliest rows created until table contains no more than 10000 rows.

I am attempting to create delete query that will meet this requirement.

Have attempted with:
delete from logtable where pkeyid < (select top(1) pkeyid from
(select top (10000) pkeyid from logtable)  order by pkeyid desc))

but MSSql report syntax errors for this attempt

Looking for corrections to my query or alternate providing same solution results
0
Comment
Question by:JoeSnyderJr
  • 4
  • 4
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24355533
this should do:
delete from logtable 
where pkeyid in (select top(10000) pkeyid from logtable order by pkeyid desc)

Open in new window

0
 

Author Comment

by:JoeSnyderJr
ID: 24355629
Sorry but that is going to delete the most current 10000 records. I am attempting to delete records NOT in that set.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24355656
delete from logtable
where pkeyid not in (select top(10000) pkeyid from logtable order by pkeyid desc)
0
 

Author Comment

by:JoeSnyderJr
ID: 24355754
Granted this will work but won't this be a resource hog compared to delete where pkeyID < TOP(1)
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24355866
that can only be answered by checking the explain plan vs the timing of the queries under similar environments...

your syntax error, btw, was here:
delete from logtable 

  where pkeyid < ( select top 1 pkeyid 

                     from ( select top 10000 pkeyid 

                              from logtable order by pkid desc 

                          ) sq  

                     order by pkeyid asc

                  ) asc

Open in new window

0
 

Author Comment

by:JoeSnyderJr
ID: 24355980
Sorry to be a pain but don't recognize meaning for seqment ') sp' and get syntax error on line 7 near 'asc'
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24356094
ousp
delete from logtable 

  where pkeyid < ( select top 1 pkeyid 

                     from ( select top 10000 pkeyid 

                              from logtable order by pkid desc 

                          ) sq  

                     order by pkeyid asc

                  ) 

Open in new window

0
 

Author Closing Comment

by:JoeSnyderJr
ID: 31580181
PERFECT!  Thanks much for prompt assistance. No wonder you are consistently at top of assistance list.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now