0

以下是表格:

emp_id    salary   salary_date
Emp1    1000    Feb 01
Emp1    2000    Feb 15
Emp1    3000    Feb 28
Emp1    4000    Mar 01
Emp2    5000    Jan 01
Emp2    6000    Jan 15
Emp2    2000    Mar 01
Emp2    5000    Apr 01
Emp3    1000    Jan 01
Emp4    3000    Dec 31
Emp4    5000    Dec 01

我想要以下结果:

Emp1  Feb           3000 
Emp2  Jan           6000 
Emp4  Dec           5000 
Emp2  Apr           5000 
Emp1  Mar           4000 
4

3 回答 3

0
SELECT e.Emp_ID,MaxSalary,MonthName
FROM employeeTable e
INNER JOIN
(
SELECT MAX(salary) as MaxSalary,
       LEFT(salary_date,3) as MonthName
FROM employeeTable
GROUP BY LEFT(salary_date,3)
)t
ON e.Salary=t.MaxSalary
于 2014-04-30T18:43:15.423 回答
0

这是我使用漂亮的RANK() 函数对它的看法:

SELECT emp_id, to_char(salary_date, 'MM, YYYY'), salary FROM (
  SELECT emp_id, salary_date, salary,
  rank() over (partition BY to_char(salary_date, 'MM-YYYY') ORDER BY salary DESC) rnk
  FROM emp_salary) WHERE rnk = 1

输出:

EMP_ID  MONTHYEAR   SALARY
Emp2    01, 2014    6000
Emp1    02, 2014    3000
Emp1    03, 2014    4000
Emp2    04, 2014    5000
Emp4    12, 2014    5000

这是用于处理数据和查询的 SQL Fiddle:http ://sqlfiddle.com/#!4/e58eaa/32

于 2014-04-30T19:40:57.300 回答
0

我找到了答案!!!

select emp_id, substr(sal_date,1,3) 每月,salary from (select emp_id, sal_date,salary, max(salary)over (partition by substr(sal_date,1,3)) max_sal from emp_salary order by emp_id) wheresalary=最大萨尔;

结果集:

EMP_ID 月薪


Emp1 4000 年 3 月 Emp1 3000 年 2 月 Emp2 6000 年 1 月 Emp2 5000 年 4 月 Emp4 5000 年 12 月

于 2014-05-01T04:30:20.827 回答