简体   繁体   中英

set flag who have count greater than a specified number

I have a SQL table to store notification and each user need to see latest 5 notification and need to soft delete remaining notifications(as per the request from client).

In the attached image I need to set IsDeleted flag to 1 for each user who have more than 5 notifications(need to sort in descending order of ID and set flag who have count greater than 5) 在此处输入图片说明

I think you want:

with toupdate as (
      select n.*,
             row_number() over (partition by username order by id desc) as seqnum
      from notifications n
     )
update toupdate
    set isdeleted = 1
    where seqnum > 5;

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