I guess your code basically has a function like merge_users($useridtokeep, $useridtogetridof).
Mostly, you just need to update the mdl_quiz_attempts table, to set userid to the new value.
That is, something like
UPDATE {quiz_attempts} SET userid = $useridtokeep WHERE userid = $useridtogetridof
Except that won't always work, because (quizid, userid, attempt) needs to be unique. That is, if both $useridtokeep and $useridtogetridof have made an attempt 1 at the quiz, then you will need to rename one of them to attempt 2. Probably best to do that according to ORDER BY timestart, or something like that.
Then, once you have combined the two sets of attempts, you need to re-compute the final grades. The best way to do that is to call quiz_update_all_final_grades for all quizzes that were affected.
Finally, you need to deal with quiz_overrides. Again, the only tricky case is if currently an override exists for both $useridtokeep and $useridtogetridof. You need to decide a way to merge the two records. (Perhaps just throw one away.)
Oh, and it is probably a good idea to do
UPDATE {question_attempt_steps} SET userid = $useridtokeep WHERE userid = $useridtogetridof
No problems with unique keys there.
Oh, and also
UPDATE {question} SET createdby = $useridtokeep WHERE createdby = $useridtogetridof
UPDATE {question} SET modifiedby = $useridtokeep WHERE modifiedby = $useridtogetridof
Again, no complexity there