我在MySQL中有一个表,每年都有学生注册。我想制作一个查询,显示在yyyy出生的nnn学生结束aaaa的学年。我的查询工作得非常快,但是让我感到困惑的部分是我在数据库中有一些重复的条目,我需要从总计中拒绝。
到目前为止,这是我的查询。并且,正如我所说,我知道它允许重复值多年。
SELECT COUNT(YEAR(birthdate)) as YearCount, YEAR(birthdate) as BirthYear, AcadYearEnding
FROM Enrollment
GROUP BY AcadYearEnding
ORDER BY AcadYearEnding, BirthYear
结果最终看起来像(如果我输入WHERE子句来限制特定学生):
YearCount BirthYear AcadYearEnding
1 2007 2012
2 2007 2013
1 2007 2014
1 2007 2015
这是使用WHERE子句生成上述数据的查询...但我最终会删除WHERE子句以显示所有学生但没有任何学生的重复条目。
SELECT COUNT(YEAR(birthdate)) as YearCount, YEAR(birthdate) as BirthYear, AcadYearEnding
FROM Enrollment
WHERE StudentId=16183
GROUP BY AcadYearEnding
ORDER BY AcadYearEnding, BirthYear
鉴于我的where子句限制单个学生,我知道2013条目是数据库中额外行的结果(并且清理数据不是一个选项,我们需要限制它查询)。
我尝试做一个子选择,给我一个唯一学生ID的列表......但在2013年,它发现学生ID两次,所以它仍然计算两次。
以下是生成错误结果的原始数据,因为2013年有2条记录:
RecId StudentId Birthdate AcadYearEnding
39885 16183 11/29/2005 2012
33768 16183 11/29/2005 2013
31020 16183 11/29/2005 2013
59508 16183 11/29/2005 2014
64054 16183 11/29/2005 2015
RecId是表格的唯一标识符。
我可能需要的是以下内容(但我很感激有人确认):
SELECT COUNT(YEAR(birthdate)) as YearCount, YEAR(birthdate) as BirthYear, AcadYearEnding
FROM (SELECT DISTINCT(StudentId), birthdate, AcadYearEnding From Test) As Ed
GROUP BY AcadYearEnding
ORDER BY AcadYearEnding, BirthYear
答案 0 :(得分:0)
我只想要count(distinct)
,我想:
SELECT COUNT(DISTINCT StudentId) as YearCount, YEAR(birthdate) as BirthYear, AcadYearEnding
FROM Enrollment
WHERE StudentId = 100510230
GROUP BY AcadYearEnding
ORDER BY AcadYearEnding, BirthYear;
当然,这也适用于没有WHERE
子句:
SELECT COUNT(DISTINCT StudentId) as YearCount, YEAR(birthdate) as BirthYear, AcadYearEnding
FROM Enrollment
GROUP BY AcadYearEnding
ORDER BY AcadYearEnding, BirthYear;
答案 1 :(得分:0)
SELECT
COUNT(DISTINCT StudentId) AS YearCount,
YEAR(birthdate) AS BirthYear,
AcadYearEnding
FROM (SELECT
StudentId,
AcadYearEnding,
BirthYear
FROM Enrollment
GROUP BY StudentId,
AcadYearEnding) AS t
GROUP BY AcadYearEnding
ORDER BY AcadYearEnding, BirthYear;
正如您所指出的,您可以使用限制来测试。不需要使用where子句,因为limit会为您提供更广泛的测试范围。 (尝试使用LIMIT 10;)。当然,这取决于您在特定年份获得的数据量。
答案 2 :(得分:0)
所以我在问题底部提出的(可能的)答案很接近......但不太正确。实际答案结果是:
SELECT (COUNT(YEAR(Birthdate)) As YearCount, Year(Birthdate) As BirthYear, AcadYearEnding
From (SELECT DISTINCT(StudentId), AcadYearEnding, Birthdate From Enrollment) As Ed
GROUP BY BirthYear, AcadYearEnding
ORDER BY AcadYearEnding, BirthYear
我已经完成了对Birthdate的计数...这可能,也许应该是StudentId,但计数是相同的。
稍微进行一些测试,但我非常感谢帮助实现这一目标。