Sybase tricky sql -- How to delete the earliest 2 records?

Table:

 

create table BE_TES_FEED (

    TRADE_STATUS VARCHAR(1) NOT NULL

    ,INSERT_DATE DATETIME NOT NULL

) lock datarows

go

 

Data:

 

INSERT INTO BE_TES_FEED VALUES ('1', getDate())

INSERT INTO BE_TES_FEED VALUES ('2', getDate())

INSERT INTO BE_TES_FEED VALUES ('3', getDate())

INSERT INTO BE_TES_FEED VALUES ('4', getDate())

INSERT INTO BE_TES_FEED VALUES ('5', getDate())

INSERT INTO BE_TES_FEED VALUES ('6', getDate())

INSERT INTO BE_TES_FEED VALUES ('7', getDate())

INSERT INTO BE_TES_FEED VALUES ('8', getDate())

INSERT INTO BE_TES_FEED VALUES ('9', getDate())

INSERT INTO BE_TES_FEED VALUES ('10', getDate())

 

sql:

 

DELETE FROM BE_TES_FEED

WHERE INSERT_DATE IN

(

SELECT INSERT_DATE

FROM BE_TES_FEED T2 

WHERE 2 > (SELECT COUNT(*) FROM BE_TES_FEED  T3 WHERE T3.INSERT_DATE < T2.INSERT_DATE)

)

 

The core idea:

 

Pay attention to the yellow part, and the data.

 

There is 0 record earlier than the 1 record.

There is 1 record earlier than the 2 record.

There are 2 records earlier than the 3 record.

… …

 

Show me all those records, with this number less than 2, and delete them!!

 

你可能感兴趣的:(sql,Sybase,idea,Go)