Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Site Related Forums
 Article Discussion
 Update on Returning a Row Number in a Query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AskSQLTeam
Ask SQLTeam Question

USA
0 Posts

Posted - 08/21/2001 :  15:16:55  Show Profile  Visit AskSQLTeam's Homepage  Reply with Quote
Mark writes "graz on 12/5/2000 12:01:00 AM in Queries wrote:

SELECT emp_id, lname, fname, job_id,
(SELECT COUNT(*) FROM employee e2 WHERE e2.lname <= e.lname AND e2.job_id = 10) AS rownumber
FROM employee e
WHERE job_id = 10
ORDER BY lname

This is an interesting option that bears a little closer scrutiny. It runs a count of records that have lname less than or equal to the current lname value. This works as long as lname is unique. Otherwise you get duplicate row numbers.

It works even if lname isn't unique if you do this:

SELECT emp_id, lname, fname, job_id,
(SELECT COUNT(*) FROM employee e2 WHERE ((e2.lname < e.lname) OR (e2.lname = e.lname AND e2.emp_id <= e.emp_id)) AND e2.job_id = 10) AS rownumber
FROM employee e
WHERE job_id = 10
ORDER BY lname"

Edited by - robvolk on 08/21/2001 15:21:11

robvolk
Most Valuable Yak

USA
15732 Posts

Posted - 08/21/2001 :  15:22:15  Show Profile  Visit robvolk's Homepage  Reply with Quote
Here's graz's original article:

http://www.sqlteam.com/item.asp?ItemID=1491

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2019 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.52 seconds. Powered By: Snitz Forums 2000