加入表时出错

时间:2011-09-27 08:37:48

标签: sql sql-server-2005

我收到关键字'WHERE'

附近的错误,语法不正确

我想要做的就是使用另一个表中的一些列

SELECT  unitCode, studentID, s.studentFName 
FROM    Student As S Right outer Join       
      (SELECT unitCode, studentID, 
          SUM(CASE WHEN subStatus = 'Yes' THEN 1 ELSE 0 END) AS CountYes, 
          SUM(CASE WHEN subStatus = 'No' THEN 1 ELSE 0 END) AS CountNo
       FROM Assignment 
       GROUP BY unitCode, studentID) 
WHERE (CountNo > 0) AND (CountYes = 0) AND s.studentID = assignment.studentID 

2 个答案:

答案 0 :(得分:3)

您的联接缺少其ON clause,并且子选择缺少别名:

SELECT
    unitCode, studentID, s.studentFName 
FROM
    Student As S
    Right outer Join       
    (
        SELECT
            unitCode, studentID, 
            SUM(CASE WHEN subStatus = 'Yes' THEN 1 ELSE 0 END) AS CountYes, 
            SUM(CASE WHEN subStatus = 'No' THEN 1 ELSE 0 END) AS CountNo
        FROM
            Assignment 
        GROUP BY unitCode, studentID
    ) as assignment 
    on s.studentID = assignment.studentID 
WHERE 
    (CountNo > 0) AND (CountYes = 0)
;

答案 1 :(得分:2)

子选择必须在SQL Server中有别名。

改变这个:

OUTER JOIN (SELECT ...)

到此:

OUTER JOIN (SELECT ...) T1