Select Top N Records Ordered by X, But Have Results in Reverse Order

前端 未结 4 1540
执念已碎
执念已碎 2020-12-11 15:50

I\'m trying to get the top N records (when ordered by some column X), but have the result set in reverse order. The following statement is incorrect, but pr

4条回答
  •  庸人自扰
    2020-12-11 16:21

    ORDER BY clause is used to order the RESULT SET by a specified column.

    Your query Select TOP 10 * from FooTable ORDER BY X DESC assuming X is the timestamp ,is not going to return the most recently inserted 10 rows. It will return the top 10 rows as stored (in whichever order) in the Database, and will then return the result set of the 10 such rows , in descending order. Hence your subquery should be modified to

    Select TOP 10 * from (Select * from FooTable ORDER BY DESC) as T

    This should fulfill your first requirement. You may then use this result set as an alias, to decide your final sort order.

    I hope i have understood you correctly, when you say "I'm trying to get the top N records (when ordered by some column X)"

提交回复
热议问题