BIGINT UNSIGNED value is out of range in …_BIGINT UNSIGNED

在SQL练习的时候报了:MySQLdb._exceptions.OperationalError: (1690, "BIGINT UNSIGNED value is out of range in '(`t`.`rn` - `t`.`dn`)'")题目链接:https://www.nowcoder.com/practice/b626ff9e2ad04789954c2132c74c0512?tpId=82&rp=1&ru=%2Fta%2Fsql&qru=%2Fta%2Fsql%2Fquestion-ranking原因是rn 和 dn两个值是无符号(unsigned )数值且大小不一,相减存在负数的情况,所以需要将rn , dn 转换成有符号(signed)数值再进行取绝对值计算

select id,job,score,dn as 'rank' from (
    select id,
    job,
    score,
    rank() over(partition by job order by score asc) as rn,
    rank() over(partition by job order by score desc) as dn,
    count(score) over(partition by job) as total
    from grade ) as t
    where (total%2=1 and rn=dn) or (total%2=0 and abs(cast(rn as signed)-cast(dn as signed))=1 )
order by id