如何在 SQL Server 中使用 row_number()
我想更新列 (p_id
) 的 row_number
为 1 的行数据..但此语法提供错误:
update app1
set p_id = 1
where Row_Number() = 1 over(p_id)
I want to update row data where the row_number
of the column (p_id
) is 1.. but this syntax is providing error:
update app1
set p_id = 1
where Row_Number() = 1 over(p_id)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能直接使用
ROW_NUMBER()
- 您需要使用 CTE(通用表表达式):为了使用
ROW_NUMBER
函数,您还需要至少有一个 ORDER BY 子句来定义行的排序顺序。从您的问题来看,您不太清楚您想要按照什么标准(列)进行排序来确定您的
ROW_NUMBER()
,也不清楚有什么类型的列来唯一标识一行(所以可以应用UPDATE
)You can't use
ROW_NUMBER()
directly - you need to e.g. use a CTE (Common Table Expression) for that:In order to use the
ROW_NUMBER
function, you also need at least anORDER BY
clause to define an order by which the rows are ordered.From your question, it's not very clear what criteria (column) you want to order by to determine your
ROW_NUMBER()
, and it's also not clear what kind of column there is to uniquely identify a row (so that theUPDATE
can be applied)这只会更新该年龄的第一位员工。可用作彩票类型逻辑
This will update only the first employee of that age. May be used as a lottery type logic
TO 删除重复项 ;WITH CTE(姓名,地址1,电话,RN)
作为
(
选择姓名、地址1、电话、
ROW_NUMBER() OVER(按名称分区 ORDER BY 名称) AS RN
)
从 CTE 中删除,其中 RN > 1
TO Delete duplicates ;WITH CTE(Name,Address1,Phone,RN)
AS
(
SELECT Name,Address1,Phone,
ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Name) AS RN
)
DELETE FROM CTE WHERE RN > 1