2013-05-07 88 views
-1

我在表中有大約一百萬行數據。我想刪除最後3行中的每5行。通過查詢刪除SQL中的行

例子:

Row1 
Row2 
Row3 
Row4 
Row5 
Row6 
Row7 
Row8 
Row9 
Row10 

之後,我刪除它們就應該是這樣的:

Row1 
Row2 
Row6 
Row7 

我會怎樣做呢?

回答

0

這應該這樣做,但您需要指定order by中的某一列。 而且應該有其他一些規則來刪除這些記錄。

DROP TABLE _test 
GO 
create table _test 
(id int identity(1,1) 
,x int 
) 
GO 
insert into _test(x) 
select 1 
go 50 


;with cte as (
    select [rwn] = row_number() over(partition by 1 order by id) 
    from _test 
) 
delete from cte 
where 1=1 
    and rwn%5 not in (1,2) 

select * from _test 
0

使用此語句。它是作品

delete from temp12 where Reg_No in (select Reg_No from 
(select Reg_No,Name, Row_number() over (order by reg_no) abc from temp12) 
where abc like '%3' or abc like '%4' or abc like '%5' or abc like '%8' or abc like '%9' or abc like '%0')