TSQL 说明
TSQL Clarification
我在 select 语句中有以下行:
cast (row_number( ) OVER ( ORDER BY cs.OrgId ) AS int) AS rowId
有人可以确认我是否理解正确吗?
按 Org Id 排序并创建一个新列 rowId 作为 int 保存订单中 orgid 位置的值?
听起来对吗?
不,你说的不准确。函数 row_number() OVER (ORDER BY [order])
returns 一个值,表示如果行按指定顺序排序,该行将占据的位置。
这种细微差别意味着几件事:
- 您可以使用不同的顺序(和排名函数)获得不同的行号(或其他排名)
- 您可以根据任何其他标准对最终结果集进行排序,不同于
row_number()
函数
- 您可以使用
row_number()
(或任何其他 window 函数)的结果来过滤、分组或任何您想用它做的事情
cast
部分仅从BIGINT
转换为INT
。
而 "creates a new column" 取决于您在哪里使用 window 函数。例如,您可以在 where
子句中使用它,在这种情况下它根本不会创建新列。
您可以阅读documentation on OVER clause which has full explanations, and several samples. See also the ranking functions docs。
我在 select 语句中有以下行:
cast (row_number( ) OVER ( ORDER BY cs.OrgId ) AS int) AS rowId
有人可以确认我是否理解正确吗?
按 Org Id 排序并创建一个新列 rowId 作为 int 保存订单中 orgid 位置的值?
听起来对吗?
不,你说的不准确。函数 row_number() OVER (ORDER BY [order])
returns 一个值,表示如果行按指定顺序排序,该行将占据的位置。
这种细微差别意味着几件事:
- 您可以使用不同的顺序(和排名函数)获得不同的行号(或其他排名)
- 您可以根据任何其他标准对最终结果集进行排序,不同于
row_number()
函数 - 您可以使用
row_number()
(或任何其他 window 函数)的结果来过滤、分组或任何您想用它做的事情
cast
部分仅从BIGINT
转换为INT
。
而 "creates a new column" 取决于您在哪里使用 window 函数。例如,您可以在 where
子句中使用它,在这种情况下它根本不会创建新列。
您可以阅读documentation on OVER clause which has full explanations, and several samples. See also the ranking functions docs。