2011-06-19 39 views
-3

对于如我有如下表如何确定工资比平均薪水高

id name city salary dept 

,我想选择哪个都大于平均

感谢

+3

你必须要说点什么..它是什么。一个DataBase表或什么? – ub1k

+2

我认为它的罪犯标记这个'算法' – kyun

回答

11

尝试是这样的所有薪水:

SELECT salary WHERE salary > (SELECT AVG(salary) FROM *) 
4

假设它是mysql,只有下面两个工作。 (我用了一个临时表,因此名称是从你的不同)

select * from b where ref > (select avg(ref) from b); 
select * from b having ref > (select avg(ref) from b); 

这不 - select * from b having ref > avg(ref);

有些疑问我试过 -

mysql> select * from b; 
+------+------------+------+ 
| id | d2   | ref | 
+------+------------+------+ 
| 300 | 2010-12-12 | 3 | 
| 300 | 2011-12-12 | 2 | 
| 300 | 2012-12-12 | 1 | 
| 400 | 2011-12-12 | 1 | 
+------+------------+------+ 
4 rows in set (0.00 sec) 

mysql> select * from b having ref > avg(ref); 
+------+------------+------+ 
| id | d2   | ref | 
+------+------------+------+ 
| 300 | 2010-12-12 | 3 | 
+------+------------+------+ 
1 row in set (0.00 sec) 

mysql> select * from b having ref > (select avg(ref) from b); 
+------+------------+------+ 
| id | d2   | ref | 
+------+------------+------+ 
| 300 | 2010-12-12 | 3 | 
| 300 | 2011-12-12 | 2 | 
+------+------------+------+ 
2 rows in set (0.02 sec) 

mysql> select * from b where ref > (select avg(ref) from b); 
+------+------------+------+ 
| id | d2   | ref | 
+------+------------+------+ 
| 300 | 2010-12-12 | 3 | 
| 300 | 2011-12-12 | 2 | 
+------+------------+------+ 
2 rows in set (0.00 sec) 

mysql> select *,avg(ref) from b having ref > avg(ref); 
+------+------------+------+----------+ 
| id | d2   | ref | avg(ref) | 
+------+------------+------+----------+ 
| 300 | 2010-12-12 | 3 | 1.7500 | 
+------+------------+------+----------+ 
1 row in set (0.00 sec) 
1

如果开窗聚合函数的支持:

SELECT Salary 
FROM (
    SELECT 
    Salary, 
    AVG(Salary) OVER() AS AvgSalary 
    FROM atable 
) s 
WHERE Salary > AvgSalary 
0
select empno,e.deptno,sal 
    from emp e, (select deptno,avg(sal) avsal 
        from emp 
       group by deptno 
      ) a 
where e.sal > a.avsal 
    and e.deptno = a.deptno; 
+0

得到一个雇员的薪水比从部门得到的平均薪水 – Krishna

1

它真的很容易只是使用遵循以下

SELECT *FROM table_name WHERE salary > avg(select salary from table_name) 

希望得到您的IT :-)

0

如果表的名称是员工(ID,姓名,城市,工资)

给出简短的命令
select salary from Employee where salary > (select ava(salary) from employee) 
0

假设EMP是表的名称,其部门ID为dept_id

  1. 查询结果显示所有员工的工资大于该部门平均工资的详细信息。 (部门明智)

(集团由部门)

select e1.* from emp e1 inner join (select avg(sal) avg_sal,dept_id from emp group by 
dept_id) as e2 on e1.dept_id=e2.dept_id and e1.sal>e2.avg_sal 
  • 查询结果显示所有雇员细节其薪水比平均薪水高。

    select * from emp where sal > (select avg(sal) from emp) 
    
  • 相关问题