简体   繁体   中英

Update statement using aggregate function in sql

In an interview they asked me to write query "update the rows that has maximum percentage"...the column consists of studentName,maths,science,percentage ...

Please help me to write update query using aggregate function...

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

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)
;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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM