×

Loading...

顽固分子们,group by 只是对结果进行分类,只是显示顺序不一样,对结果没有影响的。

如果实在要用group by , try
select * from employee t1 where t1.empsalary >
(select avg(empsalary) from employee t2 where t2.depname = t1.depname ) group by t1 depname
Ely 的statment 有两处严重错误 1.Invalid object name 'EMP'
2.在 avg 后 用GROUP BY B.DEPTNAME Incorrect syntax near the keyword 'WHERE'.
让我详细讲解我的statement的含义吧.
把employee 映射到 t1, t2,这样指针在t1,t2中的移动就不会互相影响,t1 中的记录会每一条扫一遍,而每扫一条t1的记录, 就会在t2对应t1.depname 将t2中相同depname做avg. 所以要是某个dep有10个员工,就会分别扫t1 符合条件的记录一次, 扫t2 每条符合条件的记录10 次! ,t1中的记录和avg比较后,符合条件的就会放到结果集中,再用group by 或order by 分组或排序.
Report

Replies, comments and Discussions: