2011-06-19 59 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) 
    
  • 相關問題