x

How to order result set by row number

Is there any easier way to order result set by row number than below?

SELECT * FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY A.id) AS rownum, * 
FROM MyTable A
) B
ORDER BY B.rownum

Thanks .

more ▼

asked May 17, 2010 at 02:33 PM in Default

avatar image

Stan
81 10 10 15

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Remove the subquery.

SELECT *,ROW_NUMBER() OVER(ORDER BY A.id) AS rownum
FROM MyTable A
ORDER BY rownum
more ▼

answered May 17, 2010 at 02:38 PM

avatar image

CirqueDeSQLeil
5.5k 11 13 20

  • : Simple enough!

May 17, 2010 at 02:59 PM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left

And if you don't want the rownumber as a column

SELECT *
FROM MyTable A
ORDER BY ROW_NUMBER() OVER(ORDER BY A.id)
more ▼

answered May 17, 2010 at 02:58 PM

avatar image

Kev Riley ♦♦
65.6k 48 63 81

  • simple as well.

May 17, 2010 at 03:02 PM CirqueDeSQLeil
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1084
x28

asked: May 17, 2010 at 02:33 PM

Seen: 7269 times

Last Updated: May 17, 2010 at 02:39 PM

Copyright 2017 Redgate Software. Privacy Policy