获取结果集sql server中记录的行号

2022-12-31数据库问题
4

本文介绍了获取结果集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中记录的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12