用于从不同时段的同一表中选择数据的SQL查询

时间:2012-12-18 11:49:11

标签: sql oracle oracle11g plsqldeveloper

我想在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. 年内受雇或被解雇的个人没有出现
  3. 注意:

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

    有人可以帮忙吗?非常感谢您的协助

1 个答案:

答案 0 :(得分: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