使用MSSQL计算唯一出现的id

时间:2013-04-16 20:13:17

标签: sql-server ms-access

我正在尝试计算每年独特的老师身份。我相信结果不是唯一的。

我的查询

SELECT DISTINCT count (tblTeachers.TeacherID) AS uniqueTeacherCount, tblSchoolYears.SchoolYear

FROM tblTeachers INNER JOIN tblTeacher_Building 
.
.

GROUP BY tblSchoolYears.SchoolYear
ORDER BY tblSchoolYears.SchoolYear DESC;

结果(计算TeacherID的副本)

enter image description here

我也试过

SELECT COUNT(DISTINCT tblTeachers.TeacherID) AS uniqueTeacherCount, tblSchoolYears.SchoolYear

FROM tblTeachers INNER JOIN tblTeacher_Building 
.
.

GROUP BY tblSchoolYears.SchoolYear
ORDER BY tblSchoolYears.SchoolYear DESC;

我错过了什么?

谢谢

2 个答案:

答案 0 :(得分:3)

Select Count(TeacherID) AS uniqueTeacherCount,SchoolYear
from
(
SELECT DISTINCT tblTeachers.TeacherID, tblSchoolYears.SchoolYear
FROM tblTeachers INNER JOIN tblTeacher_Building 
.
.
) hlp
GROUP BY SchoolYear
ORDER BY SchoolYear DESC;

答案 1 :(得分:1)

你很亲密。我想这就是你要找的东西:

SELECT COUNT(*)
FROM
(
SELECT DISTINCT count (tblTeachers.TeacherID) AS uniqueTeacherCount, tblSchoolYears.SchoolYear ...
)