1
SELECT campus,semester, AVG(CountOfStudents)
FROM
(
  SELECT semester,year,campus, count(*) as CountOfStudents
  FROM regestration
  GROUP BY  semester, year, campus,student_id
) t
GROUP BY   campus,semester

我需要做的是找到每个校区每学期的平均人数

我的表结构是:

表名 -注册

student_id
campus
year
batch
semester

campus, year,semesterbatch这些可以帮助识别唯一记录,其中student_id可能会重复上面的查询给出错误的答案。

4

1 回答 1

1

跟着这些步骤:

  • student_IDGROUP BY子句中删除和
  • 添加DISTINCT里面COUNT()

询问,

SELECT campus, semester, AVG(CountOfStudents)
FROM
(
  SELECT semester, year, campus, count(DISTINCT student_id) as CountOfStudents
  FROM   registration
  GROUP BY  semester, year, campus
) t
GROUP BY   campus, semester
于 2012-12-26T10:29:27.730 回答