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 probably demonstrates what I'm after:
SELECT * FROM (SELECT TOP 10 * FROM FooTable ORDER BY X DESC) ORDER BY X ASC
For example, column X could be an ID or a timestamp; I want the latest 10 records but want them returned in forward chronological order.
SELECT * FROM
(SELECT TOP 10 * FROM FooTable ORDER BY X DESC) as myAlias
ORDER BY X ASC
i.e. you might need an alias on your subquery, but other than that it should work.
An alternate solution to this question for all non-supported versions for TOP keyword is to use LIMIT. Example :-
SELECT * FROM
(SELECT * FROM FooTable ORDER BY X DESC LIMIT 10) as myAlias
ORDER BY X ASC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With