2014-09-26 37 views
0

下面是在我计算值四个属性查询..如何把我的查询合并到一个单一的查询(或可能是一个存储过程。)

1. 
Here I am calculating the values for chrg_orig 


    with ep as 
     (select emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm') as pay_month, 
     max(code), 
     max(bill) as bill, 
     max(chrg) as charge, 
     sum(nvl(pay_1,0)) sum_pay1, 
     sum(nvl(pay_2, 0)) sum_pay2, 
     (chrg_mon*22)+ (chrg_week*5)+ chrg_day as days, 
     from emp_payments 
     where emp_cd in ('HP','2000') 
     and code  in ('X','Y','Z') 
     group by emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm'), 
     code 
    ) 
    select emp_cd, 
     emp_num, 
     pay_month, 
     max(code), 
     sum(bill) 
     case when sum(days)=22 then sum(chrg) else round((round(sum(chrg)/sum(days),4)*22),2) end as chrg_orig 
    from ep 
    where chrg <>0 
    group by 
    emp_cd, 
    emp_num, 
    paymonth 

---------------------------------------------------------------------------------------- 
2. 
Here I am calculating the values for rate_chrg 

    with ep as 
     (select emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm') as pay_month, 
     code, 
     max(bill) as bill, 
     max(chrg) as charge, 
     sum(nvl(pay_1,0)) sum_pay1, 
     sum(nvl(pay_2, 0)) sum_pay2, 
     (chrg_mon*22)+ (chrg_week*5)+ chrg_day as days, 
     from emp_payments 
     where emp_cd in ('HP','2000') 
     and code  in ('X','Y','Z') 
     group by emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm'), 
     code 
    ) 
    (
    select a.emp_cd,a.emp_num,a.key,b.rate as rate_chrg from 
     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,max(invc_dt) as invc_dt from ep 
      where code in ('X','Y') and rate <> 0 
      group by emp_cd,emp_num,to_char(invc_dt,'yyyymm')) a, 

     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,invc_dt,rate from ep 
      where code in ('X','Y') and rate <> 0) b 
    where a.emp_cd = b.emp_cd 
    and a.emp_num = b.emp_num 
    and a.key = b.key 
    and a.invc_dt = b.invc_dt 
    ) 



----------------------------------------------------------------------------------------- 
3. 
Here I am calculating the values for bonus_chrg 




    with ep as 
     (select emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm') as pay_month, 
     code, 
     max(bill) as bill, 
     max(chrg) as charge, 
     sum(nvl(pay_1,0)) sum_pay1, 
     sum(nvl(pay_2, 0)) sum_pay2, 
     (chrg_mon*22)+ (chrg_week*5)+ chrg_day as days, 
     from emp_payments 
     where emp_cd in ('HP','2000') 
     and code  in ('X','Y','Z') 
     group by emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm'), 
     code 
    ) 
    (
    select a.emp_cd,a.emp_num,a.key,b.rate as bonus_chrg from 
     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,max(invc_dt) as invc_dt from ep 
      where code in ('Z') and rate <> 0 
      group by emp_cd,emp_num,to_char(invc_dt,'yyyymm')) a, 

     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,invc_dt,rate from ep 
      where code in ('Z') and rate <> 0) b 
    where a.emp_cd = b.emp_cd 
    and a.emp_num = b.emp_num 
    and a.key = b.key 
    and a.invc_dt = b.invc_dt 
    ) 

------------------------------------------------------------------------------------------ 
4. 
Here I am calculating the values for comp_days 



    with ep as 
     (select emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm') as pay_month, 
     code, 
     max(bill) as bill, 
     max(chrg) as charge, 
     sum(nvl(pay_1,0)) sum_pay1, 
     sum(nvl(pay_2, 0)) sum_pay2, 
     (chrg_mon*22)+ (chrg_week*5)+ chrg_day as days, 
     from emp_payments 
     where emp_cd in ('HP','2000') 
     and code  in ('X','Y','Z') 
     group by emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm'), 
     code 
    ) 
    (
    select emp_cd,emp_num,paymonth as key,sum(days) as comp_days from ep 
    where code in ('X','Y') 
    group by emp_cd,emp_num,key 
    ) 

-------------------------------------------------- 

我迄今所做是,我已将所有这些单独的查询放入ETL工具中,并使用chrg_orig作为驱动表执行左外连接,并为非匹配列指定零。 但我想我需要一个完整的外连接(我无法在工具中实现它)并为所有不匹配的值分配零。

我想将这些查询分组到一个查询中。什么是解决它的最佳方法? 所有的输入和建议都是有价值的。感谢..

回答

1

您可以通过用逗号分隔CTE定义来创建带有CTE的more than one virtual table。而且,CTE可以指其他CTE。

假设ep在所有这些查询一样,你可以做这样的事情:

with ep as 
    (select emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm') as pay_month, 
     max(code), 
     max(bill) as bill, 
     max(chrg) as charge, 
     sum(nvl(pay_1,0)) sum_pay1, 
     sum(nvl(pay_2, 0)) sum_pay2, 
     (chrg_mon*22)+ (chrg_week*5)+ chrg_day as days, 
    from emp_payments 
    where emp_cd in ('HP','2000') 
    and code  in ('X','Y','Z') 
    group by emp_cd, 
     emp_num, 
     to_char(pay_dt,'yyyymm'), 
     code 
    ), 
chrg_orig (<field names here>) as (
    select emp_cd, 
    emp_num, 
    pay_month, 
    max(code), 
    sum(bill) 
    case when sum(days)=22 then sum(chrg) else round((round(sum(chrg)/sum(days),4)*22),2) end as chrg_orig 
    from ep 
    where chrg <>0 
    group by 
    emp_cd, 
    emp_num, 
    paymonth 
), 
rate_chrg (<field names here>) as (
    select a.emp_cd,a.emp_num,a.key,b.rate as rate_chrg from 
     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,max(invc_dt) as invc_dt from ep 
      where code in ('X','Y') and rate <> 0 
      group by emp_cd,emp_num,to_char(invc_dt,'yyyymm')) a, 

     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,invc_dt,rate from ep 
      where code in ('X','Y') and rate <> 0) b 
    where a.emp_cd = b.emp_cd 
    and a.emp_num = b.emp_num 
    and a.key = b.key 
    and a.invc_dt = b.invc_dt 
), 
bonus_chrg (<field names here>) as (
    select a.emp_cd,a.emp_num,a.key,b.rate as bonus_chrg from 
     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,max(invc_dt) as invc_dt from ep 
      where code in ('Z') and rate <> 0 
      group by emp_cd,emp_num,to_char(invc_dt,'yyyymm')) a, 

     (select emp_cd,emp_num,to_char(pay_dt,'yyyymm') as key,invc_dt,rate from ep 
      where code in ('Z') and rate <> 0) b 
    where a.emp_cd = b.emp_cd 
    and a.emp_num = b.emp_num 
    and a.key = b.key 
    and a.invc_dt = b.invc_dt 
), 
comp_days (<field names here>) as (
    select emp_cd,emp_num,paymonth as key,sum(days) as comp_days from ep 
    where code in ('X','Y') 
    group by emp_cd,emp_num,key 
) 
SELECT * 
FROM ep 
LEFT OUTER JOIN chrg_orig 
    ON <JOIN CONDITION> 
LEFT OUTER JOIN rate_chrg 
    ON <JOIN CONDITION> 
LEFT OUTER JOIN bonus_chrg 
    ON <JOIN CONDITION> 
LEFT OUTER JOIN comp_days 
    ON <JOIN CONDITION> 
+0

明白了,感谢..是不是最好的办法? – Spider 2014-09-26 15:56:54

+0

@ user2942261这很难回答,因为性能会因查询,数据等而异。可能有更好的方法来拉动数据周期,比如使用'OVER()'子句来聚合数据,这可能允许多个聚合函数在具有较少CTE或子查询的同一语句中。这取决于你的情况。 – 2014-09-26 16:02:44

相关问题