How to sort posts by the average of comment meta values

I’ve done exactly something similar. First, use get_comment_meta for getting the per comment rating, you don’t need the SQL for it. Then get the average rating (addition of individual comment ratings on a single post / number of comments on this post). Then store this average value as post meta for this post. Use WP_Query custom fields parameters for ordering these posts.