How to assign case statement that has alias to variable using sql server : In my code below I'm trying to assign the results of a CASE statement to a variable.  I got that working but my problem is that my CASE statement has an alias for the column name but I'm getting an "incorrect syntax near 'AS'".  I am needing to SUM my variables to get a GRAND TOTAL in that column as well.  I am working in SQL Server 2014.

I could not find a solution and was hoping that someone can provide some direction/help.  Thanks in advance.

Here is my SQL code:

    SELECT DISTINCT a1.SCHYEAR, a1.LocationName as LOCATIONNAME, a1.GRADE, a1.[RACE/ETHNICITY], 
	@504Count=(
	CASE
		WHEN a1.StudentPermID IN (SELECT DISTINCT t4.Permnum FROM #504 t4) THEN COUNT(a1.StudentPermID)
	END AS '504'),
	@Non504SPEDCount =(
	CASE
		WHEN a1.STID NOT IN (SELECT DISTINCT t1.STID FROM #speds t1) THEN COUNT(a1.STID)
		WHEN a1.StudentPermID NOT IN (SELECT DISTINCT t3.Permnum FROM #504 t3) THEN COUNT(a1.StudentPermID)
	END AS 'Non-504/Non-SPED'),
	@SPEDCount=(
	CASE 
		WHEN a1.STID IN (SELECT DISTINCT t2.STID FROM #speds t2) THEN COUNT(a1.STID)
	END AS 'SPED'),
	a1.STID, a1.StudentPermID as PERMNUM, COUNT('504'), COUNT('Non-SPED'), COUNT('SPED'),
	SUM(@504Count + @Non504SPEDCount + @SPEDCount) AS 'Grand Total' 
    FROM #allStudents a1
    GROUP BY a1.SCHYEAR, a1.LocationName, a1.GRADE, a1.[RACE/ETHNICITY], a1.STID, a1.StudentPermID
    ORDER BY a1.SCHYEAR, a1.LocationName, a1.GRADE, a1.[RACE/ETHNICITY], a1.STID, a1.StudentPermID

 