开发者

Get Offset of ROWID in SQLite3

How do I get the o开发者_如何学运维ffset of a particular row in a SQLite3 database? I am not talking about the ROWID, which is not linear, I am talking about the offset/position of a specific ROWID from the first ROWID in the table.

This is something like the SELECT COUNT(rowid) FROM table WHERE rowid < row, which returns the number of rows before the row identified with row. I feel that this is too slow, so I am hoping there is another way, faster than this, which will give me the offset/position of a row in a table.


select top 1
   rowid
from
   table
where
  rowid < row
order by
  rowid desc

That should give you the largest rowid before your row. You could also try...

select
   max(rowid)
from
   table
where
  rowid < row

...But in my experience the first option performs better.

If you just want the number of rows in front of your target row. Use...

SELECT COUNT(1) FROM table WHERE rowid < row

Basically what you had before, but don't specify a column or else it is going to have to check that column for nulls while counting.

Unfortunately, you don't have access to the "ROW_NUMBER()" function like there is in MS SQL.

(Similiar Question regarding SQLLite and numbering)

sqlite equivalent of row_number() over ( partition by ...?

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜