根据日期查询当前工资

时间:2016-07-08 12:40:22

标签: mysql sql oracle

id       name      salary      date
1        aaa       10000     10/2/2014
1        aaa       15000     06/04/2015
1        aaa       20000     16/07/2016
1        aaa       25000     10/04/2017

如果员工每年都加薪, 我怎样才能获得员工当年的薪水? 该查询应该适用于每年。

如果我在2013年执行它显示2013年薪水,如果在2016年执行它显示2016年薪水,同样,2017年也将显示2017年薪水。

3 个答案:

答案 0 :(得分:0)

在Mysql中你可以这样做:

select * form tablename where YEAR(now()) = YEAR(fieldname)

答案 1 :(得分:0)

检查此SQL Fiddle是否为MySQL

SELECT
  SUM(salary) as salary
FROM salary
WHERE YEAR(STR_TO_DATE(date, '%d/%m/%Y')) = YEAR(CURDATE())
GROUP BY id

答案 2 :(得分:0)

解决 ORACLE

SELECT *
FROM SALARY_TABLE
WHERE NAME = 'whatever_the_name_is'
AND TRUNC(HIKE_DATE,'YYYY') = TRUNC(SYSDATE,'YYYY');

使用虚拟数据进行示例查询

WITH SALARY_TABLE AS -- Sample data queried from DUAL
  (SELECT 1                            AS ID,
    'aaa'                              AS name,
    10000                              AS salary,
    TO_DATE('10/2/2014', 'dd/mm/yyyy') AS hike_date
  FROM dual
  UNION
  SELECT 2, 'aaa', 15000, TO_DATE('06/04/2015', 'dd/mm/yyyy') FROM dual
  UNION
  SELECT 3, 'aaa', 20000, TO_DATE('16/07/2016', 'dd/mm/yyyy') FROM dual
  UNION
  SELECT 3, 'aaa', 25000, TO_DATE('10/04/2017', 'dd/mm/yyyy') FROM dual
  )
SELECT * -- Actual solution
FROM SALARY_TABLE
WHERE NAME                     = 'aaa'
AND TRUNC(HIKE_DATE,'YYYY') = TRUNC(SYSDATE,'YYYY');