每家企业的工龄工资算法不同,所有根据各自的算法自行修改,
需求:
1.工龄基数可设置修改,(假设为每年涨200)
2.当月的1号到30号,满一年,则次月开始算工龄工资,所以在次月的下一个月工资发放时才有算工龄工资
假设 3月1日满一年,则4月份开始有加工龄工资,所有5月发工资时才有发到工龄工资。
3.每月1号定时跑批。
sql:
--判断该临时表是否存在
drop table if EXISTS tmp_emp_basic_salary;
--创建临时表
CREATE TEMPORARY TABLE tmp_emp_basic_salary AS
select
a.work_age_start_date as "age_start",--工龄起算日期
a.id as "emp_id",--员工id
b.prm_value as "work_salary",--工龄工资基数(假设每年涨200基数)
extract(year from age(CURRENT_DATE,a.work_age_start_date)) as "work_age",--计算工龄的年 基数
current_date as "now_date"--当前时间
from
hr.hr_employee a
left join hr.hr_sys_param b on a.affiliation = b.affiliation and a.del_flag='0' and prm_name='SENIORITY_WAGE'
where a.del_flag = '0'
and a.work_status <> '04'
--算工龄必须大于等于1年,这样才有工龄
--算入职时间和当前时间比较,必须在满一年后2个月才开始算工龄工资,(这个可以根据自己的业务需求计算)
and extract( year from age( date_trunc('month',CURRENT_DATE),
date_trunc('month',a.work_age_start_date)+ interval '2 month' ) ) >=1;
注解:
1.date_trunc:截断日期类型函数date_trunc(text,time/timestamp/timestamptz);
select date_trunc('month',now());--每个月的1号
date_trunc('month',a.work_age_start_date)+ interval '2 month' ) --入职那个月的1号,后两个月发工资
2.age(timstamp,timestamp):返回一个时间差,例如年龄,第一个时间减去第二个时间.如果第一个参数不写,那么默认是当前时间,第二个参数需要格式为timestamp类型
3.EXTRACT(field FROM source) :field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。
select extract(year from now());--取当前年
如果不创建临时表也是一样的,
修改对应的工资表,相关数据
update hr.hr_basic_welfare set
work_age_salary = (a.work_salary:: Integer *work_age:: Integer),--工龄工资,基数*年数
welfare_allow_total = contract_allowance+ tech_allowance +other_welfare+ (a.work_salary:: Integer *work_age:: Integer)
FROM tmp_emp_basic_salary a
where a.emp_id = hr.hr_basic_welfare.employee_id;
可以根据自己的需求制定算法