If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Need to delete first n rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-18-05, 04:23
Shefu Shefu is offline
Registered User
 
Join Date: Apr 2005
Posts: 127
Need to delete first n rows

Need to delete first n rows at a time <B>FROM A SET OF ROWS </B> that are returned, I am not able to use ROW_NUMBER() OVER() in a delete statement..

Eg.

delete from TableName where col1 > 10 and col2 <20

I wanna delete the first row from the result set returned by this delete statement.

I tried with,

delete from TableName where col1 > 10 and col2 <20 and ROW_NUMBER() OVER() < 2

but it fails..



thanks
Shefu
Reply With Quote
  #2 (permalink)  
Old 11-18-05, 05:32
gardenman gardenman is offline
Registered User
 
Join Date: Apr 2004
Posts: 54
do like this:

delete from (select * from crm.person fetch first 1000 rows only)
Reply With Quote
  #3 (permalink)  
Old 11-18-05, 10:18
juliane26 juliane26 is offline
Registered User
 
Join Date: Oct 2005
Posts: 109
But Shefu, what is the purpose of that ??
__________________
Juliane
Reply With Quote
  #4 (permalink)  
Old 11-18-05, 17:35
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by Shefu
Need to delete first n rows
If you mean the alphabetically (or numerically) first n rows when the table is sorted on column col1, do the following:
Code:
DELETE FROM TableName
WHERE  col1 < ( SELECT a.col1
                FROM TableName AS a INNER JOIN TableName AS b ON a.col1 > b.col1
                GROUP BY a.col1 HAVING count(*) = n )
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On