计算社保的基数,要求是从薪资表和人员表开始取值,给定一个年份,一个公司,求出这个公司所有人员的这个年份的社保基数,人员计算要从人员表计算,人员表每个月数据都有,要求根据这一年的最后一个月的人员进行统计,薪资表也是每个月的数据。
其实这个需求最难的不是sql,最难得是数据不全,数据中人员表的数据只有两年的数据,且两年数据不全,薪资表的数据是4年的数据,也是不全的,因为如果是完全年的话其实计算起来还是不难的,但是我手里的手里是不全的,数据还涉密,我没有办法同步。
<select id="findSdssNotificationBaseTableListProvince" parameterType="java.util.Map"
resultType="cn.chinaunicom.sdsi.talent.entity.SdssNotificationBaseTable">
select #{year} as year,
t.totalcut as total_cut,
mon.city_id,
mon.organization_id,
mon.employee_number as staff_no,
mon.last_name as staff_name,
t.grosspay as total_payable,
t.employeecount as months_payable,
t.grosspaymonths as average_monthly_payable,
t.grosspaymonths as recommended_base,
(select PER_MAX
FROM sdss.SS_BASE_LIMIT U
left join sdss.SS_KIND P
on U.SS_ID = P.ID
where SS_ID = 1
and VALID_DATE = (
select MAX(VALID_DATE)
FROM sdss.SS_BASE_LIMIT U
left join sdss.SS_KIND P
on U.SS_ID = P.ID
where SS_ID = 1
)) as threshold_up,
(select PER_MIN
FROM sdss.SS_BASE_LIMIT U
left join sdss.SS_KIND P
on U.SS_ID = P.ID
where SS_ID = 1
and VALID_DATE = (
select MAX(VALID_DATE)
FROM sdss.SS_BASE_LIMIT U
left join sdss.SS_KIND P
on U.SS_ID = P.ID
where SS_ID = 1
)) as threshold_down
from (select STAFF_NO as employee, count(STAFF_NO) as employeecount, ROUND((SUM(GROSS_PAY) - ifnull(SUM(OTHER_CUT), 0)) / COUNT(pay_salary_info.YEAR_MONTH), 2) AS grosspaymonths, ROUND(SUM(GROSS_PAY), 2) as grosspay, ifnull(ROUND(SUM(OTHER_CUT), 2), 0) as totalcut
from ods.pay_salary_info
where pay_salary_info.YEAR_MONTH like CONCAT(#{year}, '%')
and STAFF_NO in (select employee_number
from dhr.per_person_info_monthy
where `year_month` = #{month}
and organization_id in (SELECT o2.ORGANIZATION_ID
FROM dhr.g_organization o1
INNER JOIN dhr.g_organization o2
ON o2.leftnode >= o1.leftnode
AND o2.rightnode <= o1.rightnode
where o1.organization_id = #{orgid}))
group by STAFF_NO) t
left join dhr.per_person_info_monthy mon
on t.employee = mon.employee_number and `year_month` = #{month}
</select>
per_person_info_monthy人员表,pay_salary_info薪资表,
入参是三个,分别是年份,年月的人员的最后一个月,组织id,
首先需要去人员表里面去查询到人员表在某个年份的的最后一个月的数据,去薪资表里面去使用,这样就拿到的了薪资表指定公司指定年份的所有人的薪资的集合。
按照用户编号分组,求出薪资表某个人的所有薪资的和,以及个人薪资表的和/领取薪资的月份,因为不是所有的人都发了12月的工资,平均算起来没有问题这样。
然后去指定表中去去除某年的社保基数的最大值,这个后期应该会修改,目前是求得最大值,因为他之前就是取得最大值。