I have this query where I am trying to order by a case statement however I get invalid column for "Grade"??SELECT '12th' AS Grade, SUM(CASE WHEN AG_Schedule = 'Yes' THEN 1 ELSE 0 END) * 100 / COUNT(ID) AS Percentage, 'A-G Schedule' AS IndicatorFROM ewiseSchema.Grade12CH3_table(@GR12, @SC) AS Grade12CH1_table_1UNIONSELECT '11th' AS Grade, SUM(CASE WHEN AG_Schedule = 'Yes' THEN 1 ELSE 0 END) * 100 / COUNT(ID) AS Percentage, 'A-G Schedule' AS IndicatorFROM ewiseSchema.Grade11CH3_table(@GR11, @SC) AS Grade11CH1_table_1UNIONSELECT '10th' AS Grade, SUM(CASE WHEN AG_Schedule = 'Yes' THEN 1 ELSE 0 END) * 100 / COUNT(ID) AS Percentage, 'A-G Schedule' AS IndicatorFROM ewiseSchema.Grade10CH3_table(@GR10, @SC, @dt1, @dt2, @year) AS Grade10CH1_table_1UNIONSELECT '9th' AS Grade, SUM(CASE WHEN AG_Schedule = 'Yes' THEN 1 ELSE 0 END) * 100 / COUNT(ID) AS Percentage, 'A-G Schedule' AS IndicatorFROM ewiseSchema.Grade9CH3_table(@GR9, @SC, @dt1, @dt2, @year) AS Grade9CH1_table_1ORDER BY Indicator, (CASE Grade WHEN '9th' THEN 1 WHEN '10th' THEN 2 WHEN '11th' THEN 3 WHEN '12th' THEN 4 END)
Cartesian Yak