如何使用SQL Server将具有别名的case语句分配给变量

时间:2016-06-22 13:26:15

标签: sql-server case

在我的下面的代码中,我试图将CASE语句的结果分配给变量。我得到了这个工作,但我的问题是我的CASE语句有列名的别名,但我得到“AS'附近的错误语法”。

我需要SUM我的变量以在该列中获得GRAND TOTAL。我正在使用SQL Server 2014。

我找不到解决方案,并希望有人可以提供一些指导/帮助。提前谢谢。

这是我的SQL代码:

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),
    @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),
    @SPEDCount = (CASE 
                     WHEN a1.STID IN (SELECT DISTINCT t2.STID FROM #speds t2) 
                       THEN COUNT(a1.STID)
                  END),
    a1.STID, a1.StudentPermID as PERMNUM, @504Count as '504', 
    @Non504SPEDCount as 'Non-504/Non-SPED', @SPEDCount as '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

2 个答案:

答案 0 :(得分:0)

在为变量

指定值时不需要
    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

答案 1 :(得分:0)

试试这段代码,

 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),
    @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),
    @SPEDCount=(
    CASE 
        WHEN a1.STID IN (SELECT DISTINCT t2.STID FROM #speds t2) THEN COUNT(a1.STID)
    END),
    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