I am getting an error that grandvisit is an invalid identifier, and I am unsure if I will get it with other identifiers either. Any light shed on this would be amazing. As I cannot see why it differents from others.
Advertisement
Answer
You cannot refer the alias
names in same select
clause where it is generated.
SELECT Sum(aerosolvisit + hazvisit + hvvisit + hlopvisit + plvisit + miniloadvisit + aprvisit + apr2visit + aprawkvisit) AS grandvisit
To calculate grandratio
you are using the below expression which uses the alias name grandvisit
SELECT CASE WHEN Sum(grandtotal) = 0 THEN 0 WHEN Sum(grandvisit) = 0 THEN 0 ELSE Sum(grandtotal) / Sum(grandvisit) END AS grandratio
You can use derived table to do this
SELECT CASE WHEN Sum(grandtotal) = 0 THEN 0 WHEN Sum(grandvisit) = 0 THEN 0 ELSE Sum(grandtotal) / Sum(grandvisit) END AS grandratio From ( SELECT Sum(aerosolvisit + hazvisit + hvvisit + hlopvisit + plvisit + miniloadvisit + aprvisit + apr2visit + aprawkvisit) AS grandvisit ... ) A
Note : You are using old style outer join’s(+=
). It is better to use Right/Left
joins which is more readable