SQL Query无需重复即可检索结果

时间:2011-11-02 10:56:14

标签: sql-server

我有一个名为Schedule的表,其中包含名为TeacherName和ClassTakenDate的字段。表中的值如下所示:

TeacherName       ClassTakenDate

Anish              2011-10-01

Anish              2011-10-01

John               2011-10-01

John               2011-10-02

我想要这样的结果:

TeacherName               NoOfDays

Anish                        1

John                         2

我们如何做到这一点?

2 个答案:

答案 0 :(得分:2)

SELECT TeacherName, 
       COUNT(DISTINCT ClassTakenDate) AS NoOfDays
FROM Schedule 
GROUP BY TeacherName

答案 1 :(得分:0)

SELECT TeacherName, ClassTakenDate, COUNT( * ) AS NoOfDays 
FROM (
    SELECT DISTINCT TeacherName, ClassTakenDate
      FROM Schedule 
) AS foo
GROUP BY foo.ClassTakenDate