本文介紹了獲取結果集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中記錄的行號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!