日期:2014-05-16  浏览次数:20705 次

组函数 嵌套 找出平均工资大于300的部门的平均工资的最高工资
create table t_user(
  t_id integer primary key,
  t_name varchar2(12),
  t_birthday date,
  t_age integer
);


create table t_dept(
  t_id integer primary key,
  t_dname varchar2(12),
  t_dno varchar2(12)
);
create table t_employee(
  t_id integer primary key,
  t_name varchar2(12),
  t_birthday date,
  t_email varchar2(56),
  t_salary number(12,2),
  d_id integer references t_dept(t_id)
);



insert into t_dept(t_id,t_dname,t_dno) values (1,'dev','001');
insert into t_dept(t_id,t_dname,t_dno) values (2,'train','002');
insert into t_dept(t_id,t_dname,t_dno) values (3,'market','003');
insert into t_dept(t_id,t_dname,t_dno) values (4,'game','004');

insert into t_employee(t_id,t_name,t_birthday,t_email,t_salary,d_id) 
values (1,'bobs',to_date('2012-1-11','yyyy-mm-dd'),'bobs@b',100.0,1);
insert into t_employee(t_id,t_name,t_birthday,t_email,t_salary,d_id)
values (2,'scott',to_date('2010-10-11','yyyy-mm-dd'),'scott@b',300.0,1);
insert into t_employee(t_id,t_name,t_birthday,t_email,t_salary,d_id)
values (3,'tiger',to_date('2008-2-11','yyyy-mm-dd'),'tiger@b',400.0,2);
insert into t_employee(t_id,t_name,t_birthday,t_email,t_salary,d_id)
values (4,'lucy',to_date('2009-3-11','yyyy-mm-dd'),'lucy@b',600.0,2);
insert into t_employee(t_id,t_name,t_birthday,t_email,t_salary,d_id)
values (5,'lily',to_date('2012-4-11','yyyy-mm-dd'),'lily@b',1000.0,3);
insert into t_employee(t_id,t_name,t_birthday,t_email,t_salary,d_id)
values (6,'tom',to_date('2001-10-11','yyyy-mm-dd'),'tom@b',700.0,3);
insert into t_employee(t_id,t_name,t_birthday,t_email,t_salary,d_id)
values (7,'black',to_date('2001-10-11','yyyy-mm-dd'),'black@b',200.0,null);
commit;
--------------------------------
----上面是需要使用的数据-----
--找出平均工资大于300的部门的平均工资的最高工资
--问题:下面是我做得,但是不是想要的结果,求解~~~~

select max(aaa.avg_sal),aaa.avg_name from(
select avg(e.t_salary) as avg_sal,d.t_dname as avg_name,d.t_dno
from t_dept d inner join t_employee e on e.d_id=d.t_id group by d.t_dname,d.t_dno having  avg(e.t_salary)>300) aaa
group by aaa.avg_name