-2

我想在 2012 年的各个月份为我的员工获取个人工资。这是代码。

SELECT 
jd.job_grade,
e.employee_title||'. '|| e.first_name||' '||e.initials||' '||e.surname as FULLNAME,
e.employee_code,
sum(NVL(jan.amount,0)) as JAN_PAY,
sum(NVL(feb.amount,0)) as FEB_PAY,
sum(NVL(mar.amount,0)) as MAR_PAY,
sum(NVL(apr.amount,0)) as APR_PAY,
sum(NVL(may.amount,0)) as MAY_PAY,
sum(NVL(jun.amount,0)) as JUN_PAY,
sum(NVL(jul.amount,0)) as JUL_PAY,
sum(NVL(aug.amount,0)) as AUG_PAY,
sum(NVL(sep.amount,0)) as SEP_PAY,
sum(NVL(oct.amount,0)) as OCT_PAY,
sum(NVL(nov.amount,0)) as NOV_PAY,
sum(NVL(ar.amount,0)) as DEC_PAY/*,
0.05*sum(NVL(ar.amount,0)) as Bonus*/ 
from (((((((((((((tbl_actual_run ar
JOIN tbl_actual_run JAN ON jan.emp_code=ar.emp_code)
JOIN tbl_actual_run FEB ON feb.emp_code=ar.emp_code)
JOIN tbl_actual_run MAR ON mar.emp_code=ar.emp_code)
JOIN tbl_actual_run APR ON apr.emp_code=ar.emp_code)
JOIN tbl_actual_run MAY ON may.emp_code=ar.emp_code)
JOIN tbl_actual_run JUN ON jun.emp_code=ar.emp_code) 
JOIN tbl_actual_run JUL ON jul.emp_code=ar.emp_code)
JOIN tbl_actual_run AUG ON aug.emp_code=ar.emp_code)
JOIN tbl_actual_run SEP ON sep.emp_code=ar.emp_code)
JOIN tbl_actual_run OCT ON oct.emp_code=ar.emp_code)
JOIN tbl_actual_run NOV ON nov.emp_code=ar.emp_code) 
JOIN TBL_EMPLOYEE E ON E.EMPLOYEE_CODE=ar.emp_code) 
JOIN tbl_job_details jd ON jd.employee_code=ar.emp_code) 
where (ar.payroll_date LIKE '%DEC-12%' and ar.ed_code in ('0001','0011','0012','0003')) 
and  (nov.Payroll_Date LIKE'%NOV-12%'and nov.ed_code in ('0001','0011','0012','0003'))
and (oct.Payroll_Date LIKE'%OCT-12%' and oct.ed_code in ('0001','0011','0012','0003'))
and (sep.Payroll_Date LIKE'%SEP-12%' and oct.ed_code in ('0001','0011','0012','0003'))
and (aug.Payroll_Date LIKE'%AUG-12%' and aug.ed_code in ('0001','0011','0012','0003'))
and (jul.Payroll_Date LIKE'%JUL-12%' and jul.ed_code in ('0001','0011','0012','0003'))
and (jun.Payroll_Date LIKE'%JUN-12%' and jun.ed_code in ('0001','0011','0012','0003'))
and (may.Payroll_Date LIKE'%MAY-12%' and may.ed_code in ('0001','0011','0012','0003'))
and (apr.Payroll_Date LIKE'%APR-12%' and apr.ed_code in ('0001','0011','0012','0003'))
and (mar.Payroll_Date LIKE'%MAR-12%' and mar.ed_code in ('0001','0011','0012','0003'))
and (feb.Payroll_Date LIKE'%FEB-12%' and feb.ed_code in ('0001','0011','0012','0003'))
and (jan.Payroll_Date LIKE'%JAN-12%' and jan.ed_code in ('0001','0011','0012','0003'))
GROUP BY jd.job_grade,e.employee_title,e.first_name,e.initials,e.surname,e.employee_code 
order by TO_NUMBER(jd.job_grade) DESC, e.first_name,e.surname

问题是

  1. 这些数字是累积的,没有反映每个人每个月的收入情况。
  2. 一年内被雇用或解雇的个人没有出现

笔记:

  • TBL_ACTUAL_RUN 是保存所有月薪的地方
  • TBL_Employee 是保存所有员工的地方
  • TBL_JOB_Details 是保存员工工作信息的地方

有人可以帮忙吗?您的帮助将不胜感激

4

1 回答 1

1

这对于评论来说太长了,所以我将其发布为答案。我认为您将很难使用所有这些连接来调试您的查询。这是一个在没有所有连接的情况下可能更容易调试的版本。这是做同样的事情,除了您使用带有CASE语句的聚合来决定每个月。语法类似于:

SELECT jd.job_grade,
  e.employee_title||'. '|| e.first_name||' '||e.initials||' '||e.surname as FULLNAME,
  e.employee_code,
  sum(case when ar.payroll_date LIKE '%JAN-12%' then ar.amount else 0 end) JAN_PAY,
  sum(case when ar.payroll_date LIKE '%FEB-12%' then ar.amount else 0 end) FEB_PAY,
  sum(case when ar.payroll_date LIKE '%MAR-12%' then ar.amount else 0 end) MAR_PAY,
  sum(case when ar.payroll_date LIKE '%APR-12%' then ar.amount else 0 end) APR_PAY,
  sum(case when ar.payroll_date LIKE '%MAY-12%' then ar.amount else 0 end) MAY_PAY,
  sum(case when ar.payroll_date LIKE '%JUN-12%' then ar.amount else 0 end) JUN_PAY,
  sum(case when ar.payroll_date LIKE '%JUL-12%' then ar.amount else 0 end) JUL_PAY,
  sum(case when ar.payroll_date LIKE '%AUG-12%' then ar.amount else 0 end) AUG_PAY,
  sum(case when ar.payroll_date LIKE '%SEP-12%' then ar.amount else 0 end) SEP_PAY,
  sum(case when ar.payroll_date LIKE '%OCT-12%' then ar.amount else 0 end) OCT_PAY,
  sum(case when ar.payroll_date LIKE '%NOV-12%' then ar.amount else 0 end) NOV_PAY,
  sum(case when ar.payroll_date LIKE '%DEC-12%' then ar.amount else 0 end) DEC_PAY
from tbl_actual_run ar
left join TBL_EMPLOYEE E 
  ON E.EMPLOYEE_CODE=ar.emp_code 
left JOIN tbl_job_details jd 
  ON jd.employee_code=ar.emp_code) 
where ar.payroll_date LIKE '%-12%' 
  and ar.ed_code in ('0001','0011','0012','0003')
group by jd.job_grade,e.employee_title,e.first_name,e.initials,e.surname,e.employee_code
order by TO_NUMBER(jd.job_grade) DESC, e.first_name,e.surname
于 2012-12-18T12:04:56.793 回答