获取结果集sql server中记录的行号
- 作者: 张大帅最爱王小娥
- 来源: 51数据库
- 2022-12-13
问题描述
我有一张包含序列号文档的表格 - 列:
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.
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0
