Delete Top-N' Rows from a Table with some sorting(order by 'Column')

后端 未结 2 466
心在旅途
心在旅途 2020-12-24 05:25

I am having some confusion regarding Deleting the top N Rows order by some column.

I created have an example here Example at fiddle

What is wrong with these

相关标签:
2条回答
  • 2020-12-24 06:09

    Add the top 3 clause to the subselect:

    Delete from Table1 
    where id IN (
        select top 3 id 
        from Table1 
        order by id desc
    )
    
    0 讨论(0)
  • 2020-12-24 06:11

    You can use a CTE to do a faster ordered delete without the need for a separate sub query to retrieve the top 3 ids.

    WITH T
         AS (SELECT TOP 3 *
             FROM   Table1
             ORDER  BY id DESC)
    DELETE FROM T 
    
    0 讨论(0)
提交回复
热议问题