开发者

How to return "row number" of first occurrence, SQL?

                         (row number not stored in db)

------------------------
|      |     A   |     | (1)
------------------------
|      |     B   |     | (2)
------------------------
|      |     C   |     | (3)
------------------------ -----Page 1
|      |     D   |     | (4)
------------------------
|      |     E   |     | (5)
------------------------
|      |     F   |     | (6)
------------------------ -----Page 2
|      |     F   |     | (7)
------------------------
|      |     F   |     | (8)
------------------------
|      |     G   |     | (9) -----Page 3
开发者_JAVA百科

i.e. a search for "F" would return 6.

Thank You.

trying to do the firstr part of this: How do I query a SQL database to return the first "page" containing the specified data?

Found these:

sequential row numbers from query

sqlite Retrieve Rownumber in sqlite


SELECT COUNT(*)
FROM tbl
WHERE letter < 'F'

(you'll get the number minus one, so you would have to increment it by one afterwards)


try this, but I don't consider the performance

--create table
create table t(letter char(1))
go

--insert values
BEGIN TRAN
INSERT INTO t VALUES ('A')
INSERT INTO t VALUES ('B')
INSERT INTO t VALUES ('C')
INSERT INTO t VALUES ('D')
INSERT INTO t VALUES ('E')
INSERT INTO t VALUES ('F')
INSERT INTO t VALUES ('F')
INSERT INTO t VALUES ('F')
INSERT INTO t VALUES ('G')
COMMIT TRAN
GO

--and then try the select what you want
DECLARE @temp table(num int identity(1,1), letter char(1))

--use a temp table variable to store the data with row number
insert into @temp
SELECT * FROM t

--select the value
SELECT * FROM @temp WHERE letter = 'F'
GO

--drop the table finally
drop table t
GO
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜