SQL查询不同类型的员工

时间:2018-12-05 06:42:19

标签: sql oracle

我有一个查询,该查询当前按月计算净面值,毛收入和扣除总额的总和,但是现在我想查询一个计算相同属性但基于不同雇员类型的查询,我的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_PAYGROSS_EARNINGGROSS_DEDUCTION的所有必要数据。

到目前为止,我每月都有数据,例如

December, 2018, 
total Net_Pay is 2 cr, 
total gross Earn 2.5 cr, 
total gross_deduct is 50lacs 

但是现在我也希望代表Employee Type的数据, 例: 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  

1 个答案:

答案 0 :(得分:0)

要按员工类型显示数据,您需要在查询的投影中包括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
;