case语句中的列名无效

时间:2013-08-01 08:48:53

标签: sql-server tsql

我得到无效的列名称'平均'。当我写没有别名的时候它起作用了。但我的病情太多了。

SELECT  
                        ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.EMPLOYEEID,
                        Employees.EMPLOYEENAMESURNAMEFORMAT AS LastFirstName,


                        (CAST(SUM(EVALUATION) AS FLOAT)
                        /
                        (SELECT TOP 1
                                COUNT(*)
                                FROM ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES 
                                WHERE DATE BETWEEN '2013-01-01' AND '2013-12-12' AND TYPE=2 AND SUPERVISORID=1020 GROUP BY EmployeeID )) AS AVERAGE,
                        CASE  WHEN AVERAGE=1

                        THEN 'GOOD' END  AS EVALUATION



                FROM ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES INNER JOIN Employees
                ON ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.EMPLOYEEID=Employees.EmployeeID
                WHERE DATE BETWEEN '2013-01-01' AND '2013-12-12' AND TYPE=2 AND ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.SUPERVISORID=1020 AND ACTIVESTATUS=1 

                                GROUP BY 
                                ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.EMPLOYEEID,
                                EMPLOYEENAMESURNAMEFORMAT

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT *,case when average =1  then 'good' end as evaluation from(
select
ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.EMPLOYEEID,
                    Employees.EMPLOYEENAMESURNAMEFORMAT AS LastFirstName,


                    (CAST(SUM(EVALUATION) AS FLOAT)
                    /
                    (SELECT TOP 1
                            COUNT(*)
                            FROM ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES 
                            WHERE DATE BETWEEN '2013-01-01' AND '2013-12-12' AND TYPE=2 AND SUPERVISORID=1020 GROUP BY EmployeeID )) AS AVERAGE


            FROM ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES INNER JOIN Employees
            ON ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.EMPLOYEEID=Employees.EmployeeID
            WHERE DATE BETWEEN '2013-01-01' AND '2013-12-12' AND TYPE=2 AND ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.SUPERVISORID=1020 AND ACTIVESTATUS=1 

                            GROUP BY 
                            ST_LAWERP_PERFORMANCE_EVALUATION_ENTRIES.EMPLOYEEID,
                            EMPLOYEENAMESURNAMEFORMAT) as subquery