December 1, 2011 at 5:19 am
Hey, i wonder what's the most efficient way to get a "rank" of a record in a table set.
An example:
If i select * from user -> SELECT * FROM #user ORDER BY id ASC
But i would like to know what rank 'Duke' have in that specific set.
CREATE TABLE #user
(id int identity(1, 1),
userid int,
firstname VARCHAR(20) NULL,)
insert into #user (userid, firstname)VALUES (4, 'Duke')
insert into #user (userid, firstname)VALUES (5, 'David')
insert into #user (userid, firstname)VALUES (6, 'Roger')
SELECT * FROM #user
DROP TABLE #user
In this case it should return value 1
December 1, 2011 at 5:23 am
Not sure I know what you're asking. Does this help?
BEGIN TRAN
CREATE TABLE #user
(id int identity(1, 1),
userid int,
firstname VARCHAR(20) NULL,)
INSERT INTO #user (userid,firstname) VALUES (4,'Duke')
INSERT INTO #user (userid,firstname) VALUES (5,'David')
INSERT INTO #user (userid,firstname) VALUES (6,'Roger')
SELECT rowid, id, userid, firstname
FROM (SELECT ROW_NUMBER() OVER (ORDER BY id) AS rowid,
id, userid, firstname
FROM #user) a
WHERE firstname = 'Duke'
ROLLBACK
December 1, 2011 at 7:40 am
Cadavre (12/1/2011)
Not sure I know what you're asking. Does this help?
Ty, that was exactly what i was looking for!
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply