在sql中使用聚合函数更新语句

时间:2012-08-11 11:02:59

标签: sql sql-server

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

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

3 个答案:

答案 0 :(得分:4)

update  YourTable
set     col1 = 42
where   percentage = 
        (
        select  max(percentage)
        from    YourTable
        )

答案 1 :(得分:1)

SQL FIDDLE DEMO

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)

答案 2 :(得分: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;