Solved

How to locate nth item in an MSSQL table

Posted on 2009-05-11
8
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 143

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 143

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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
 
LVL 143

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 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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