I've moved this post to Forums, where you could find more help.
Viz Talk is a place for,
"Share and discuss interesting data visualizations"
I sincerely apologize for my mistake!
I realized that this is not a post for help, so I moved it back to Viz Talk.
To share my comments,
I'm impressed by your careful if statements in the calculation, such as "IFNULL" and "ELSE NULL".
Though there's no null values in your test date, thoughtful calculation does help in production data.