get row number of record in resultset sql server(获取结果集sql server中记录的行号)
问题描述
我有一张包含序列号文档的表格 - 列:
I have a table full of documents with serial numbers - columns:
- docid int
- 书写文字
- 提交日期日期时间
...我想找出特定序列号在对表的查询中的位置.
...and I'd like to find out where a particular serial number lies in a query against the table.
例如,如果我按 submitDate asc 对结果集进行排序,我可能想知道文档 34 在该排序中的位置(即它是否在位置 11?)
For example, if I ordered the resultset by submissionDate asc, I might want to know where document 34 is within that ordering (i.e. is it in position 11?)
我该怎么做?
推荐答案
看起来您正在尝试获取行号,即使它不是要返回的行之一.您可以为此使用 CTE:
It looks like you're trying to get the row number even if that's not one of the rows being returned. You can use a CTE for that:
;WITH CTE AS
(
SELECT
docid,
writing,
submissionDate,
ROW_NUMBER() OVER (ORDER BY submissionDate) AS position
FROM
My_Table
)
SELECT
docid,
writing,
submissionDate,
position
FROM
CTE
WHERE
docid = 34
这当然也需要 SQL Server 2005 或更高版本.
This also requires SQL Server 2005 or greater of course.
这篇关于获取结果集sql server中记录的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取结果集sql server中记录的行号
基础教程推荐
- 带有WHERE子句的LAG()函数 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
