SQL ROW_NUMBER函数概述

本文深入探讨了SQL中的ROW_NUMBER函数,它是SQL Server中最常用的函数之一,用于为结果集添加唯一的递增编号。文章讲解了ROW_NUMBER的基本概念,如何与ORDER BY和OVER子句配合使用,以及如何与PARTITION BY子句结合实现分组计数。通过多个示例,读者可以了解如何在实际查询中应用ROW_NUMBER函数。
摘要由CSDN通过智能技术生成

In this article, we’re going to discuss the SQL ROW_NUMBER function. This is a continuation of the SQL essential series. In this guide, I’ll explain what a window function is all about, and you’ll see sample examples to understand the concepts behind the SQL ROW_NUMBER function.

在本文中,我们将讨论SQL ROW_NUMBER函数。 这是SQL基本系列的延续。 在本指南中,我将解释窗口函数的全部含义,并且您将看到示例示例以了解SQL ROW_NUMBER函数的概念。

介绍 (Introduction)

The most commonly used function in SQL Server is the SQL ROW_NUMBER function. The SQL ROW_NUMBER function is available from SQL Server 2005 and later versions.

SQL Server中最常用的函数是SQL ROW_NUMBER函数。 SQL ROW_NUMBER函数可从SQL Server 2005和更高版本中获得。

ROW_NUMBER adds a unique incrementing number to the results grid. The order, in which the row numbers are applied, is determined by the ORDER BY expression. Most of the time, one or more columns are specified in the ORDER BY expression, but it’s possible to use more complex expressions or even a sub-query. So, it creates an ever-increasing integral value and it always starts off at 1 and subsequent rows get the next higher value.

ROW_NUMBER向结果网格添加一个唯一的递增数字。 行号的应用顺序由ORDER BY表达式确定。 在大多数情况下,在ORDER BY表达式中指定了一个或多个列,但是可以使用更复杂的表达式,甚至可以使用子查询。 因此,它将创建一个不断增加的整数值,并且始终从1开始,随后的行将获得下一个更高的值。

You can also use it with a PARTITION BY clause. But when it crosses a partition limit or boundary, it resets the counter and starts from 1. So, the partition may have values 1, 2, 3, and so on and the second partitions again start the counter from 1, 2, 3… and so on, and so forth.

您也可以将其与PARTITION BY子句一起使用。 但是,当它超出分区限制或边界时,它将重置计数器并从1开始。因此,该分区可能具有值1、2、3,依此类推,第二个分区又从1、2、3…开始计数。等等等等。

基本: (Basics:)

  1. The SQL ROW_NUMBER function is a non-persistent generation of a sequence of temporary values and it is calculated dynamically when then the query is executed.

    SQL ROW_NUMBER函数是临时值序列的非持久生成,并且在执行查询时会动态计算该函数。
  2. There is no guarantee that the rows returned by a SQL query using the SQL ROW_NUMBER function will be ordered exactly the same with each execution.

    不能保证使用SQL ROW_NUMBER函数SQL查询返回的行在每次执行时的顺序将完全相同。
  3. ROW_NUMBER and RANK functions are similar. The output of ROW_NUMBER is a sequence of values starts from 1 with an increment of 1 but whereas the RANK function, the values are also incremented by 1 but the values will repeat for the ties.

    ROW_NUMBER和RANK函数相似。 ROW_NUMBER的输出是从1开始以1递增的值序列,但是RANK函数的值也以1递增,但是对于关系,这些值将重复。
  4. If you’ve an experience with Oracle then the ROWNUM is more familiar to you. It is a Pseudo-Column. It starts off with 1 and go
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值