0

在一次采访中,他们要求我编写查询“更新具有最大百分比的行”......该列由studentName,maths,science,percentage......

请帮助我使用聚合函数编写更新查询...

4

3 回答 3

4
update  YourTable
set     col1 = 42
where   percentage = 
        (
        select  max(percentage)
        from    YourTable
        )
于 2012-08-11T11:04:39.807 回答
1

SQL 小提琴演示

create table dummy (user_id int,name varchar(50),percentage int)
insert into dummy values(1,'Amit',99);
insert into dummy values(2,'Michle',80);
insert into dummy values(3,'Naval',60);
insert into dummy values(4,'Jack',56);

update dummy set percentage='100'
where percentage=(select max(percentage) from dummy)
于 2012-08-11T11:19:39.557 回答
0
;WITH x AS 
(
  SELECT 
     studentName, maths, science, 
     r = RANK() OVER (ORDER BY percentage DESC)
  FROM dbo.table_name
)
UPDATE x 
  SET studentName = studentName + ' - Gold star'
  WHERE r = 1;
于 2012-08-11T14:15:32.827 回答