Solved

How to locate nth item in an MSSQL table

Posted on 2009-05-11
8
226 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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 to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

15 Experts available now in Live!

Get 1:1 Help Now