如何在 SQL Server 中使用 row_number()

发布于 2024-11-02 05:31:03 字数 166 浏览 2 评论 0原文

我想更新列 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-11-09 05:31:03

您不能直接使用 ROW_NUMBER() - 您需要使用 CTE(通用表表达式):

;WITH DataToUpdate AS
(
    SELECT
       SomeID,
       p_id,
       ROW_NUMBER() OVER(ORDER BY .......) AS 'RowNum'
    FROM
       dbo.app1
)
UPDATE DataToUpdate
SET p_id = 1
WHERE 
   RowNum = 1

为了使用 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:

;WITH DataToUpdate AS
(
    SELECT
       SomeID,
       p_id,
       ROW_NUMBER() OVER(ORDER BY .......) AS 'RowNum'
    FROM
       dbo.app1
)
UPDATE DataToUpdate
SET p_id = 1
WHERE 
   RowNum = 1

In order to use the ROW_NUMBER function, you also need at least an ORDER 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 the UPDATE can be applied)

追我者格杀勿论 2024-11-09 05:31:03

这只会更新该年龄的第一位员工。可用作彩票类型逻辑

create table emp(name varchar(3),Age int, Salary int, IncentiveFlag bit)

insert into emp values('aaa',23,90000,0);
insert into emp values('bbb',22,50000,0);
insert into emp values('ccc',63,60000,0);
insert into emp values('ddd',53,50000,0);
insert into emp values('eee',23,80000,0);
insert into emp values('fff',53,50000,0);
insert into emp values('ggg',53,50000,0);

update A
set IncentiveFlag=1
from 
(
        Select row_number() over (partition by Age order by age ) AS SrNo,* from emp
)A
where A.SrNo=1

This will update only the first employee of that age. May be used as a lottery type logic

create table emp(name varchar(3),Age int, Salary int, IncentiveFlag bit)

insert into emp values('aaa',23,90000,0);
insert into emp values('bbb',22,50000,0);
insert into emp values('ccc',63,60000,0);
insert into emp values('ddd',53,50000,0);
insert into emp values('eee',23,80000,0);
insert into emp values('fff',53,50000,0);
insert into emp values('ggg',53,50000,0);

update A
set IncentiveFlag=1
from 
(
        Select row_number() over (partition by Age order by age ) AS SrNo,* from emp
)A
where A.SrNo=1
绅刃 2024-11-09 05:31:03

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文