题解 | #获得积分最多的人(三)#
获得积分最多的人(三)
https://www.nowcoder.com/practice/d2b7e2a305a7499fb310dc82a43820e8?tpId=82&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%3Fpage%3D2%26tab%3DSQL%25E7%25AF%2587%26topicId%3D82
我的方法,sum里面嵌套if
with tmp as(
select id,name,sum(if(type='add',grade_num,grade_num*(-1)))sg from user join grade_info g on user.id=g.user_id
group by id,name
)
select id,name,sg from tmp where sg=(select max(sg) from tmp) order by id