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作爲驅動表執行左外連接,併爲非匹配列指定零。 但我想我需要一個完整的外連接(我無法在工具中實現它)併爲所有不匹配的值分配零。
我想將這些查詢分組到一個查詢中。什麼是解決它的最佳方法? 所有的輸入和建議都是有價值的。感謝..
明白了,感謝..是不是最好的辦法? – Spider 2014-09-26 15:56:54
@ user2942261這很難回答,因爲性能會因查詢,數據等而異。可能有更好的方法來拉動數據週期,比如使用'OVER()'子句來聚合數據,這可能允許多個聚合函數在具有較少CTE或子查詢的同一語句中。這取決於你的情況。 – 2014-09-26 16:02:44