Re the SQL - TLDR, sorry
However, is it possible that some activities don't create a grade item until the student does something? I think I've been caught by this before with Assignment. It only creates the grade item when the student goes to the submission page (it doesn't matter if they actually submit or not). That's tripped me up with custom reports before.