일or놀이/MS-SQL

Rownumber 붙여서 보기

TIGERJUNE 2006. 12. 4. 16:29
다음은 결과에 순번(rownumber)를 추가하여 보여주는 방법입니다.

USE Pubs
go
SELECT (
SELECT COUNT(*) FROM authors e2 WHERE
e2.au_id <= e.au_id
) AS rownumber
, au_id, au_lname
FROM authors e ORDER BY au_id

[실행 결과]
rownumber   au_id       au_lname
----------- ----------- ----------------------------------------
1           172-32-1176 White
2           213-46-8915 Green
3           238-95-7766 Carson
4           267-41-2394 O'Leary
5           274-80-9391 Straight
6           341-22-1782 Smith
7           409-56-7008 Bennet
8           427-17-2319 Dull
9           472-27-2349 Gringlesby
10          486-29-1786 Locksley
11       .............


출처 : Microsoft SQL Server 2000 나만의 노하우 & Tip