I have a query in which I do some calculation and then I give that column an alias. In the next column I would like to use the result of that calculation in an IF statement. MySQL did not recognise the alias when I used it as a condition but instead required me to rewrite the whole query, put some brackets around it and then carry out the condition checking.
Here is the SQL query:
SELECT StudentId
,SubjectID
, (COUNT(StudentId) / (
SELECT COUNT(SubjectID)
FROM lectureattendancein
WHERE SubjectID ='MIS4' ) * 100) AS Percentage
, IF((COUNT(StudentId) / (
SELECT COUNT(SubjectID)
FROM lectureattendancein
WHERE SubjectID ='MIS4'
)* 100) >= 80, 'ALLOWED', 'NOT ALLOWED') AS ExamAdmit
FROM attendancein
GROUP BY StudentId, SubjectID
HAVING SubjectID='MIS4'
ORDER BY StudentId ASC
I would like to use the column alias called Percentage in the IF statement