一个代码引发的学案。。
select course_id,count(distinct id)
from takes
where year=2009 and semester='Fall' group by course_id;//正确
select course_id,count(distinct id)
from takes
where year=2009 and semester='Fall';//错误
//not a single-group group function
开始以为这两没区别啊。聚集不就这么用吗?
我们使用基本聚集是,只能选择一个tuple,
而上面的例子是两个tuple,故不可以。
因此为了可以选择多个tuple,我们引入了分组聚集(group by).
1.对于select clause 与group by clause中的tuple:
- 出现在group by clause中的tuple 才可以出现在select clause 中(先存在前者,后者才可以用)
- select clause中可以出现基本聚集函数,注意不是tuple,而是aggregate function.
In other words, any attribute that is not present in the group by clause must appear only inside an aggregate function if it appears in the select clause, otherwise the query is treated as erroneous