Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to locate nth item in an MSSQL table

Posted on 2009-05-11
8
Medium Priority
?
266 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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 2000 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

Independent Software Vendors: 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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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