with a as (select *, floor(points / 9999) fl, 9999 x, mod(points, 9999) md from f0511)
select id,
points,
x,
(points - sum(y) over (partition by id order by rn)) thelast
from (select *, row_number() over (partition by id) rn
from (select a1.id, a1.points, a1.x, 9999 y
from a a1,
a a2
where a2.id <= a1.fl
union all
select id, points, md, 9999
from a
order by id) t) t1;
select
t1.id
,t1.points
,case
when t1.num1 = t2.help_topic_id then t1.num2
else x1
end as x
,points - sum(x1)over(partition by id order by t2.help_topic_id) as thelast
from
(
select *,floor(points / 9999) as num1,points % 9999 as num2,9999 as x1
from F0511
)t1
left join mysql.help_topic t2 on t1.num1 >= t2.help_topic_id