Oracle Query用于获取每个员工的CURRENT CTC(薪水)

时间:2010-05-20 11:40:08

标签: oracle

我希望每个员工的当前CTC是我的表的设计

Ecode      Implemented Date            Salary
7654323    2010-05-20                  350000
7654322    2010-05-17                  250000
7654321    2003-04-01                  350000
7654321    2004-04-01                  450000
7654321    2005-04-01                  750000
7654321    2007-04-01                  650000

我希望oracle查询以下输出

Ecode          Salary
7654321        650000   
7654322        250000 
7654323        350000

提前致谢

另见

Oracle Query for getting MAximum CTC (Salary) of Each Employee

3 个答案:

答案 0 :(得分:3)

如果您想保留按ecode排序的每个implemented_date的最后一个薪水:

SQL> WITH data AS (
  2     SELECT 7654323 Ecode, '2010-05-20' Implemented_Date, 350000 Salary
  3       FROM DUAL
  4     UNION ALL SELECT 7654322, '2010-05-17', 250000 FROM DUAL
  5     UNION ALL SELECT 7654321, '2003-04-01', 350000 FROM DUAL
  6     UNION ALL SELECT 7654321, '2004-04-01', 450000 FROM DUAL
  7     UNION ALL SELECT 7654321, '2005-04-01', 750000 FROM DUAL
  8     UNION ALL SELECT 7654321, '2007-04-01', 650000 FROM DUAL
  9  )
 10  SELECT ecode,
 11         MAX(salary)
 12             KEEP (dense_rank FIRST ORDER BY Implemented_Date DESC) sal
 13    FROM DATA
 14   GROUP BY ecode;

     ECODE        SAL
---------- ----------
   7654321     650000
   7654322     250000
   7654323     350000

答案 1 :(得分:1)

SELECT * 
FROM salary s
INNER JOIN 
  (SELECT ecode, MAX(implemented_date) as implemented_date 
   FROM salary GROUP BY ecode) curr
ON curr.ecode = s.ecode AND curr.implemented_date = s.implemented_date

答案 2 :(得分:1)

我会使用分析功能。您希望为每个ecode和Impledate组合选择salary的第一个值,该命令由implementeddate排序,以将最新值设置在顶部。

select
    distinct
    first_value(ecode) OVER (PARTITION BY ecode ORDER BY IMPLEMENTEDDATE DESC NULLS LAST) Ecode, 
    first_value(implementeddate) OVER (PARTITION BY ecode ORDER BY IMPLEMENTEDDATE DESC NULLS LAST) ImplementedDate,
    first_value(salary) OVER (PARTITION BY ecode ORDER BY IMPLEMENTEDDATE DESC NULLS LAST) Salary
from 
    tbl_Salary;

“DISTINCT”将保留空行,否则将返回我们正在过滤的其他3个版本的Ecode = 7654321。

结果是:

ECODE  IMPLEMENTEDDATE  SALARY
-----  ---------------  ------
7654321     01/04/2007      650000
7654322     17/05/2010      250000
7654323     20/05/2010      350000