日期:2014-05-17  浏览次数:20805 次

求一个简单的单表查询~~~
table-student
id   age  classId
1     20    2
2     18    3
求:查询
平均学生的年龄大于  本班  平均年龄的学生信息  

------解决方案--------------------
你是不是查询 年龄大于  本班  平均年龄的学生信息   
------解决方案--------------------
select * from stutent where age > (select avg(age) from stutent);
------解决方案--------------------
引用:
select * from stutent where age > (select avg(age) from stutent);

你的年龄是每班的平均年龄吗? 你的age是和同班的平均年龄比的吗?

------解决方案--------------------
select * from lian l where age > (select avg(age) from lian where classid = l.classid);

------解决方案--------------------
select * from stutent s where age > (select avg(age) from stutent where classid = s.classid);
------解决方案--------------------
select *,avg(age)over(partition by classid) avg_age where age>avg_age
 from student;

------解决方案--------------------
楼主的问题看不懂啊!真心看不懂
------解决方案--------------------
SQL> select id, age, student.classid from student, ( select avg(age) avgage, classid from student group by classid ) class where age > class.avgage and student.classid = class.classid;

表内容:
SQL> select * from student;
 
        ID        AGE    CLASSID
---------- ---------- ----------
         1         20          2
         2         18          3
         3         20          3

结果:
        ID        AGE    CLASSID
---------- ---------- ----------
         3         20          3
------解决方案--------------------
大致思路如下:
1.先用分组函数求每个班级的平均年龄
2.全表查询班级表,然后用每个人的年龄和第1步中的平均年龄做比较

with tb_a as (
     select 1 id ,30 age ,1 classid from dual