Monday, March 19, 2012

How to reverse a reverse order by?

Hi,
using the statement
SELECT TOP 10 * FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
I get the last 10 rows in the Import table as expected, but how to I reverse
these last 10 rows?
MortenUse an outer query which has an ORDER BY in the other direction.
SELECT ...
FROM
(
SELECT TOP 10 ... FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
) AS i
ORDER BY rowID ASC
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Morten Wennevik" <Morten.Wennevik@.email.adr> wrote in message
news:op.sz2pzfp2g1d8xu@.tr023.bouvet.no...
> Hi,
> using the statement
> SELECT TOP 10 * FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
> I get the last 10 rows in the Import table as expected, but how to I rever
se these last 10 rows?
> Morten|||Hi Morten,
hope I undertstand you right to reorder the queried rows before.
SELECT
* FROM
(
SELECT TOP 10 * FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
) SubQuery
Order by RowID
So you want the hightest 10 Rows, but those in an ascending order,
right ?
HTH, Jens Suessmeyer.|||Well, I tried this before, but I get an error near the keyword 'ORDER'
SELECT * FROM
(
SELECT TOP 10 * FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
)
ORDER BY RowID ASC
Morten
On Fri, 11 Nov 2005 12:31:40 +0100, Tibor Karaszi <tibor_please.no.email_kar
aszi@.hotmail.nomail.com> wrote:

> Use an outer query which has an ORDER BY in the other direction.
> SELECT ...
> FROM
> (
> SELECT TOP 10 ... FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
> ) AS i
> ORDER BY rowID ASC|||It accepts the query when I add AS i after (), but it still does not ascend
the RowIDs
On Fri, 11 Nov 2005 12:38:16 +0100, Morten Wennevik <Morten.Wennevik@.email.a
dr> wrote:

> Well, I tried this before, but I get an error near the keyword 'ORDER'
> SELECT * FROM
> (
> SELECT TOP 10 * FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
> )
> ORDER BY RowID ASC
> Morten
>
> On Fri, 11 Nov 2005 12:31:40 +0100, Tibor Karaszi <tibor_please.no.email_k
araszi@.hotmail.nomail.com> wrote:
>
>|||Yes, given RowIDs from 1-499 I want 490-499
On Fri, 11 Nov 2005 12:37:11 +0100, Jens <Jens@.sqlserver2005.de> wrote:

> Hi Morten,
> hope I undertstand you right to reorder the queried rows before.
> SELECT
> * FROM
> (
> SELECT TOP 10 * FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
> ) SubQuery
> Order by RowID
> So you want the hightest 10 Rows, but those in an ascending order,
> right ?
> HTH, Jens Suessmeyer.
>|||You need to name the derived (inner) table, see the AS clause after the clos
ing bracket.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Morten Wennevik" <Morten.Wennevik@.email.adr> wrote in message
news:op.sz2ql2d7g1d8xu@.tr023.bouvet.no...
> Well, I tried this before, but I get an error near the keyword 'ORDER'
> SELECT * FROM
> (
> SELECT TOP 10 * FROM Import WHERE RowID >= 1 ORDER BY RowID DESC
> )
> ORDER BY RowID ASC
> Morten
>
> On Fri, 11 Nov 2005 12:31:40 +0100, Tibor Karaszi
> <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote:
>
>|||Using your suggestion the RowIDs still got from 499-490, which is the opposi
te of what I want
On Fri, 11 Nov 2005 12:41:41 +0100, Morten Wennevik <Morten.Wennevik@.email.a
dr> wrote:

> Yes, given RowIDs from 1-499 I want 490-499
>
> On Fri, 11 Nov 2005 12:37:11 +0100, Jens <Jens@.sqlserver2005.de> wrote:
>
>|||Ok, I'm baffled, selecting * ignores the last reverse ordering, but selectin
g just RowID will give me 490-499
?
On Fri, 11 Nov 2005 12:47:07 +0100, Morten Wennevik <Morten.Wennevik@.email.a
dr> wrote:

> Using your suggestion the RowIDs still got from 499-490, which is the oppo
site of what I want
> On Fri, 11 Nov 2005 12:41:41 +0100, Morten Wennevik <Morten.Wennevik@.email
.adr> wrote:
>
>

No comments:

Post a Comment