sql - 不同类型员工的sql查询
问题描述
我有一个查询,目前正在计算每月净面值、总收入和总扣除额的总和,但现在我想要一个计算相同属性但基于不同员工类型的查询,我的 Web 应用程序目前正在使用下面提到的员工类型
EMP_TYPE EMP_TYPE_NAME
01 Regular Employee
04 Deputated In Employee
06 Contractual Employee
03 Outsource Employees
05 Regular Employee-MPSIDC
现在我有另一个表 i,e EMPLOYEE_SALARY_SUMMARY
,其中包含有关薪水 i,e NET_PAY
, GROSS_EARNING
,的所有必要数据GROSS_DEDUCTION
。
到目前为止,我每月都有数据,例如
December, 2018,
total Net_Pay is 2 cr,
total gross Earn 2.5 cr,
total gross_deduct is 50lacs
但现在我也想要代表员工类型的数据,例如:2018 年 12 月
Regular Employee Net_pay =50 lacs, Gross Earn= 75 lacs, gross Deduct=25 Lac
Deputed-In Employee Net_pay =50 lacs, Gross Earn= 75 lacs, gross Deduct=25 Lac
Contractual Employee Net_pay =50 lacs, Gross Earn= 75 lacs, gross Deduct=25 Lac
我写了这个但我没有得到预期的输出,现在我只为普通员工使用这个案例,即Emp_TYPE= 1
select
count(distinct ess.EMPLOYEE_ID)emp_count,
SUM(case when et.EMP_TYPE_ID = 1 then ess.NET_SALARY else 0 end )net_sal,
SUM(case when et.EMP_TYPE_ID = 1 then ess.GROSS_EARNING else 0 end )gross_earn,
SUM(case when et.EMP_TYPE_ID = 1 then ess.GROSS_DEDUCTION else 0 end )gross_deduct
from
employee_salary_summary ess
JOIN employee e
on e.EMP_ID=ess.EMPLOYEE_ID
JOIN EMPLOYEE_TYPE et
ON et.EMP_TYPE_ID=e.EMP_TYPE_ID
group by ess.NET_SALARY, ess.GROSS_EARNING, ess.GROSS_DEDUCTION;
还建议我编写相同查询的有效方法,因此它在任何情况下都不会失败。
电流输出:
emp_count net_pay gross_earn gross_deduct
1 26862 30643 3781
1 37229 42361 5132
1 66304 75222 8918
1 19124 22300 3176
2 93144 105840 12696
4 93176 106444 13268
1 0 0 0
1 38434 43722 5288
1 0 0 0
2 81996 93240 11244
它还获取其他员工类型,如您在输出中看到的,Emp_Type 不限于 1,查询还获取其他员工类型,如 2 和 4。
预期输出应该是
emp_count Employee type net_pay gross_earn gross_deduct
51 Regular Emp 15,00000 17,000000 2,00000
26 Deputed-In 8,00000 1,00000 7,00000
29 contractual emp 6,00000 2,00000 4,00000
解决方案
要按员工类型显示数据,您需要包含EMP_TYPE_NAME
在查询的投影中。要获得正确的总数,您需要对未聚合的列进行 GROUP BY。
您没有提供任何关于如何得出工资月份的线索,所以我将其省略了,但您可能希望将其添加到 WHERE 子句(或者可能是投影,但它不在您的预期输出中):
select
et.EMP_TYPE_NAME
count(distinct ess.EMPLOYEE_ID)emp_count,
SUM(ess.NET_SALARY) net_sal,
SUM(ess.GROSS_EARNING)gross_earn,
SUM(ess.GROSS_DEDUCTION)gross_deduct
from
employee_salary_summary ess
JOIN employee e
on e.EMP_ID=ess.EMPLOYEE_ID
JOIN EMPLOYEE_TYPE et
ON et.EMP_TYPE_ID=e.EMP_TYPE_ID
/* where ess.pay_month = '2018-DEC' ??? */
group by et.EMP_TYPE_NAME
;
推荐阅读
- python - 从离散信号计算 FFT 特征
- javascript - jQuery - 索引不断重置
- asp.net-mvc-4 - 寻找解决 FIPS 兼容错误的方法
- c++ - 为什么 sizeof 在 C++ 中给出不正确的字节数?
- amazon-web-services - 检查 Cloudfront 正在从哪个 AWS 服务端点提取静态文件的任何方法
- angular - API 在需要打印到网页的数据中有 \
- android - Android Studio gradle 同步失败:constraintlayout-solver
- xml - 无法使用 XML::LibXML 在 Perl 中附加 XML 文档的文本
- css - 无法在 Wordpress 移动导航菜单上的搜索框中输入
- arrays - 如果存在 X + Y = S,则在数组中查找的最佳方法