dbTalk Databases Forums  

Row number in a select statement

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss Row number in a select statement in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Roji. P. Thomas
 
Posts: n/a

Default Re: Row number in a select statement - 02-16-2005 , 04:59 AM






use pubs
GO
--SELECT * FROM jobs
Select job_desc, (Select Count(*) + 1 FROM jobs B
WHERE B.job_desc < A.job_desc) AS RecNo
FROM jobs A
ORDER By job_desc


--
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com


"Ashok" <akumar (AT) buildfolio (DOT) com> wrote

Quote:
Hi

I am looking for a solution to get an incremental row number along my
other
select arguments after joining more than one tables.

Thanks in advance.
Ashok






Reply With Quote
  #2  
Old   
Ashok
 
Posts: n/a

Default Row number in a select statement - 02-16-2005 , 04:59 AM






Hi

I am looking for a solution to get an incremental row number along my other
select arguments after joining more than one tables.

Thanks in advance.
Ashok




Reply With Quote
  #3  
Old   
David Portas
 
Posts: n/a

Default Re: Row number in a select statement - 02-16-2005 , 05:42 AM



Why can't you do it client-side? That's probably much more efficient
than attempting it in SQL.

If you really must, the following is one example of a SQL-based method
(from Pubs). You've said that more than one table is involved but it's
hard to give a full answer for that more complex scenario without more
info - like the DDL for the tables involved and some sample data to
work with.

SELECT au_id, au_lname, au_fname,
(SELECT COUNT(*)
FROM Authors
WHERE au_id <= A.au_id) AS row_num
FROM Authors AS A

--
David Portas
SQL Server MVP
--


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.