如何使用 ROW_NUMBER()?
我想使用 ROW_NUMBER()
来获取...
- 获取
max(ROW_NUMBER())
--> 所有行的计数
或者我想这也将是我尝试执行的
SELECT max(ROW_NUMBER() OVER(ORDER BY UserId)) FROM Users
:但它似乎不起作用...
- 使用给定的信息获取
ROW_NUMBER()
,即。 如果我有一个名字,并且我想知道这个名字来自哪一行。
我认为它会类似于我尝试的#1,
SELECT ROW_NUMBER() OVER(ORDER BY UserId) From Users WHERE UserName='Joe'
但这也不起作用......
有什么想法吗?
I want to use the ROW_NUMBER()
to get...
- To get the
max(ROW_NUMBER())
--> Or i guess this would also be the count of all rows
I tried doing:
SELECT max(ROW_NUMBER() OVER(ORDER BY UserId)) FROM Users
but it didn't seem to work...
- To get
ROW_NUMBER()
using a given piece of information, ie. if I have a name and I want to know what row the name came from.
I assume it would be something similar to what I tried for #1
SELECT ROW_NUMBER() OVER(ORDER BY UserId) From Users WHERE UserName='Joe'
but this didn't work either...
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
对于第一个问题,为什么不直接使用呢?
得到计数。
对于第二个问题,行的主键是用来标识特定行的。 不要尝试为此使用行号。
如果您在主查询中返回 Row_Number() ,
那么当您想要返回 5 行时,您可以获取当前行号并使用以下查询来确定 currentrow -5 的行
For the first question, why not just use?
to get the count.
And for the second question, the primary key of the row is what should be used to identify a particular row. Don't try and use the row number for that.
If you returned Row_Number() in your main query,
Then when you want to go 5 rows back then you can take the current row number and use the following query to determine the row with currentrow -5
虽然我同意其他人的观点,您可以使用
count()
来获取总行数,但以下是如何使用row_count()
:要获取总行数:
获取名称为 Matt 的行号:
您可以进一步使用
min(rownum)
或max(rownum )
分别获取 Matt 的第一行或最后一行。这些是
row_number()
的非常简单的实现。 您可以使用它进行更复杂的分组。 查看我对不使用子查询的高级分组Though I agree with others that you could use
count()
to get the total number of rows, here is how you can use therow_count()
:To get the total no of rows:
To get the row numbers where name is Matt:
You can further use
min(rownum)
ormax(rownum)
to get the first or last row for Matt respectively.These were very simple implementations of
row_number()
. You can use it for more complex grouping. Check out my response on Advanced grouping without using a sub query如果您需要返回表的总行数,可以使用
SELECT COUNT(*)
语句的替代方法。由于 SELECT COUNT(*) 会进行全表扫描以返回行计数,因此对于大型表来说可能需要很长时间。 在这种情况下,您可以使用 sysindexes 系统表。 有一个
ROWS
列,其中包含数据库中每个表的总行数。 您可以使用以下 select 语句:这将大大减少查询所需的时间。
If you need to return the table's total row count, you can use an alternative way to the
SELECT COUNT(*)
statement.Because
SELECT COUNT(*)
makes a full table scan to return the row count, it can take very long time for a large table. You can use thesysindexes
system table instead in this case. There is aROWS
column that contains the total row count for each table in your database. You can use the following select statement:This will drastically reduce the time your query takes.
ROW_NUMBER()
为每行返回一个从 1 开始的唯一编号。您只需编写以下代码即可轻松使用它:ROW_NUMBER()
returns a unique number for each row starting with 1. You can easily use this by simply writing:您可以使用它来获取第一条记录 where has 子句
You can use this for get first record where has clause
可能与这里的问题无关。 但我发现它在使用
ROW_NUMBER
时可能很有用 -May not be related to the question here. But I found it could be useful when using
ROW_NUMBER
-您可以使用
Row_Number
来限制查询结果。例子:
-
通过上面的查询,我将从
TABLENAME
中获得第 1 页的结果。You can use
Row_Number
for limit query result.Example:
--
With above query, I will get PAGE 1 of results from
TABLENAME
.如果您绝对想为此使用 ROW_NUMBER (而不是 count(*)),您可以随时使用:
If you absolutely want to use ROW_NUMBER for this (instead of count(*)) you can always use:
需要使用
WITH table AS
创建虚拟表,这在给定的查询中提到。通过使用这个虚拟表,您可以对
row_number
执行CRUD操作。查询:
尽管使用了
SELECT
,但您仍可以在最后一句中使用INSERT
、UPDATE
或DELETE
。Need to create virtual table by using
WITH table AS
, which is mention in given Query.By using this virtual table, you can perform CRUD operation w.r.t
row_number
.QUERY:
You can use
INSERT
,UPDATE
orDELETE
in last sentence by in spite ofSELECT
.SQL Row_Number() 函数 是对相关记录集中的数据行进行排序并分配序号。 因此它用于对行进行编号,例如识别订单金额最高的前 10 行或识别金额最高的每个客户的订单等。
如果您想对数据集进行排序并通过分隔每一行进行编号我们使用 Row_Number() 和 Partition By 子句将它们分成类别。 例如,对每个客户内部的订单进行排序,其中数据集包含所有订单等。
但据我了解,您想要计算按列分组的行数。 为了可视化需求,如果您希望除了订单信息之外以单独的列形式查看相关客户的所有订单计数,您可以使用 COUNT() 带有 Partition By 子句的聚合函数
例如,
SQL Row_Number() function is to sort and assign an order number to data rows in related record set. So it is used to number rows, for example to identify the top 10 rows which have the highest order amount or identify the order of each customer which is the highest amount, etc.
If you want to sort the dataset and number each row by seperating them into categories we use Row_Number() with Partition By clause. For example, sorting orders of each customer within itself where the dataset contains all orders, etc.
But as I understand you want to calculate the number of rows of grouped by a column. To visualize the requirement, if you want to see the count of all orders of the related customer as a seperate column besides order info, you can use COUNT() aggregation function with Partition By clause
For example,
此查询:
将返回
UserName
为'Joe'
的所有行,除非您没有UserName='Joe'
它们将按顺序列出
UserID
和row_number
字段将从 1 开始并递增,但许多行包含UserName='Joe'
如果它不适合您,那么您的
WHERE
命令有问题或者表中没有UserID
。 检查UserID
和UserName
字段的拼写。This query:
will return all rows where the
UserName
is'Joe'
UNLESS you have noUserName='Joe'
They will be listed in order of
UserID
and therow_number
field will start with 1 and increment however many rows containUserName='Joe'
If it does not work for you then your
WHERE
command has an issue OR there is noUserID
in the table. Check spelling for both fieldsUserID
andUserName
.