人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

June 29, 2006

使用AVG()

Filed under: SQL&DB Accessing

有两张表:
Student: stdudentid (int), name(string)
Scroe: stdudentid (int), score
Score表中会有多个老师给学生打的分数,

现在我想查出所有的学生,以及他们的平均分数,该如何写sql语句?
我瞎搞了一个
Select st.Name, AVG(sc.Score) from student as st join Score as sc on st. stdudentid = st. stdudentid

高手答曰:

Select st.studentid, st.name, AVG (sc.Score) from student as st inner join Score as sc on st. stdudentid = sc. stdudentid Group by st. stdudentid, st.name
我怎么没想到呢?

考虑有的学生没有分数,改进为:

Select st.studentid, st.name, AVG (IsNull(sc.Score,0)) from student as st LEFT JOIN Score as sc on st. stdudentid = sc. stdudentid Group by st. stdudentid, st.name

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/06/29/p118/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham