如何从员工表中找到谁获得最高工资百分比?

时间:2016-06-25 05:37:07

标签: sql oracle

这里是emp表: 显示在01-jan-81和31-dec-81之间获得最高%薪水的员工

  EMPNO ENAME             SAL HIREDATE
------- ---------- ---------- ---------
  7369 SMITH             800 17-DEC-80
  7499 ALLEN            1600 20-FEB-81
  7521 WARD             1250 22-FEB-81
  7566 JONES            2975 02-APR-81
  7654 MARTIN           1250 28-SEP-81
  7698 BLAKE            2850 01-MAY-81
  7782 CLARK            2450 09-JUN-81
  7788 SCOTT            3000 09-DEC-82
  7839 KING             5000 17-NOV-81
  7844 TURNER           1500 08-SEP-81
  7876 ADAMS            1100 12-JAN-83
  7900 JAMES             950 03-DEC-81
  7902 FORD             3000 03-DEC-81
  7934 MILLER           1300 23-JAN-82

6 个答案:

答案 0 :(得分:0)

Array
(
    [ok] => 1
    [result] => Array
        (
            [message_id] => 599
            [from] => Array
                (
                    [id] => 224181375
                    [first_name] => boofeh
                    [username] => boofehbot
                )

            [chat] => Array
                (
                    [id] => 50408323
                    [first_name] => MOƎIN
                    [username] => imoein1
                    [type] => private
                )

            [date] => 1466751591
            [text] => اکنون سفارش خود را ارسال کنید
        )

)

薪水最高工资的员工也有给定期间的最高工资。

PS:这是通用的sql语法

答案 1 :(得分:0)

执行此操作的典型方法是使用order by和某种限制。在Oracle 12c +中,您可以执行以下操作:

select t.*
from emptable t
where hiredate between date '1981-01'01' and date '1981-12-31'
order by sal desc
fetch first 1 row only;

在早期版本中,您可以使用子查询:

select x.*
from (select t.*
      from emptable t
      where hiredate between date '1981-01'01' and date '1981-12-31'
      order by sal desc
     ) x
where rownum = 1;

注意:这仅显示一名员工。如果存在关系,则选择任意员工。要获取所有这些内容,请使用rank()dense_rank()

select x.*
from (select t.*, dense_rank() over (order by sal desc) as seqnum
      from emptable t
      where hiredate between date '1981-01'01' and date '1981-12-31'
      order by sal desc
     ) x
where seqnum = 1;

答案 2 :(得分:0)

首先,我们创建一个临时表,其中包含指定日期中每个人的所有薪水,然后我们选择使用最多的人flask-wtf

max

答案 3 :(得分:0)

以防你想打印%薪水

SELECT * FROM (SELECT emp.*, round((salary/ (SELECT SUM(salary) FROM emp ))*100,2) pcent FROM emp WHERE hiredate BETWEEN DATE '1981-01-01' AND DATE '1981-12-31' ORDER BY pcent DESC ) WHERE ROWNUM<=1;

答案 4 :(得分:0)

您可以使用KEEP ( DENSE_RANK [FIRST|LAST] ORDER BY ... )获取最多两列:

SELECT MAX( EName ) KEEP ( DENSE_RANK LAST ORDER BY Salary ) AS EName,
       100 * MAX( Salary ) / SUM( Salary ) AS Percentage_Of_Total_Salary
FROM   Employees
WHERE  HIRE_DATE >= DATE '1981-01-01'
AND    HIRE_DATE <  DATE '1982-01-01';

答案 5 :(得分:0)

这样的事情应该有效。

with selected_rows as (
       select empno, ename, sal
       from   scott.emp
       where  hiredate between date '1981-01-01' and date '1981-12-31'
     ),
     max_and_total_sal (max_sal, total_sal) as (
       select max(sal), sum(sal)
       from selected_rows
     )
select empno, ename, sal, to_char(100*sal/total_sal, '99.99') || '%' as percent_sal
from selected_rows cross join max_and_total_sal
where sal = max_sal;

Result:

     EMPNO ENAME             SAL PERCENT_SAL
---------- ---------- ---------- -----------
      7839 KING             5000      21.91%

1 row selected.

请注意,该百分比是根据1981年雇用的员工的工资总和(而不是表中所有工资的总和)计算的。作为一个快速验证,显然King拥有所有员工的最高工资(因此也是最高的百分比),事实上他在1981年被聘用,所以他必须是(#sle; WHO&#34;问题的一部分。

另请注意,解决方案取决于知道表中的hiredate列仅存储&#34;纯日期&#34; (也就是说,时间设置为00:00:00);否则......和...之间的&#34;&#34;部分必须改进。