From version 2.2, the storage of stats object has been changed. A compatibility solution was put into place to confound null classname with classname containing 'internal'. However, it appears that old database may contains the same record (same name and number) with and without the 'internal' classname. This cause the R40000XWIKI6990 migration to fails, since ids for the two record result to the same value.
To properly clean a MySQL database before migrating to 4.x, you may use the following SQL statements:
delete s1 from xwikistatsdoc s1 join xwikistatsdoc s2
on s1.XWS_NUMBER=s2.XWS_NUMBER and s1.XWS_NAME=s2.XWS_NAME
where s1.XWS_CLASSNAME = 'internal'
and (s2.XWS_CLASSNAME != 'internal' or s2.XWS_CLASSNAME is null);
update xwikistatsdoc set XWS_CLASSNAME = "" where XWS_CLASSNAME = 'internal';
delete s1 from xwikistatsreferer s1 join xwikistatsreferer s2
on s1.XWR_NUMBER=s2.XWR_NUMBER and s1.XWR_NAME=s2.XWR_NAME
where s1.XWR_CLASSNAME = 'internal'
and (s2.XWR_CLASSNAME != 'internal' or s2.XWR_CLASSNAME is null);
update xwikistatsreferer set XWR_CLASSNAME = "" where XWR_CLASSNAME = 'internal';
delete s1 from xwikistatsvisit s1 join xwikistatsvisit s2
on s1.XWV_NUMBER=s2.XWV_NUMBER and s1.XWV_NAME=s2.XWV_NAME
where s1.XWV_CLASSNAME = 'internal'
and (s2.XWV_CLASSNAME != 'internal' or s2.XWV_CLASSNAME is null);
update xwikistatsvisit set XWV_CLASSNAME = "" where XWV_CLASSNAME = 'internal';