2011-09-27 93 views

回答

4

如果学生的信息是在学生的表,然后查询可能是这样的:

SELECT student_name, student_birth_day, studentnum 
FROM Student S 
RIGHT JOIN (
    SELECT studentnum, count(*) as cnt 
    FROM Attendance 
    WHERE (attStatus = 'Yes') 
    AND (unitCode = 'MMA1034') 
    GROUP BY studentnum 
    HAVING (COUNT(*) < 4) 
) A 
ON A.studentnum = S.studentnum 
+1

天才!!!!!!!!! – David

0

GROUP BY (Transact-SQL)

在 列表中的任何非聚合表达式中的每个表或视图的列必须包含在GROUP BY列表

所以,你必须将其包含在GROUP BY如果它在选择列表中未分类。

所以,如果你想有student_name在选择列表中,未聚集,那么你就需要像

SELECT studentnum, 
     student_name 
FROM Attendance 
WHERE (attStatus = 'Yes') 
AND  (unitCode = 'SIT103') 
AND  (CONVERT(VARCHAR, attDate, 101) < '10/10/2011') 
GROUP BY studentnum, 
      student_name 
HAVING (COUNT(*) < 4) 
-2

如果student_name在同一个表中存在您需要studentnum后通过它像

SELECT studentnum,student_name 

如果student_name存在于不同的表中,则需要使用连接。

+0

这并不在ANSI SQL工作,您必须在GROUP BY中具有选定的列,或者在其上使用聚合函数。 – castaway