码迷,mamicode.com
首页 > 其他好文 > 详细

20150827

时间:2015-08-27 21:05:08      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

select CALC_MONTH,
EMPLOYEE_CODE,
max(LAST_PREMIUM) LAST_PREMIUM,
max(MONTHFEE_ONJOB)MONTHFEE_ONJOB,
max(YEARFEE_ONJOB) YEARFEE_ONJOB
from
(select distinct
SUBSTR(‘20150601‘, 1, 6) calc_month,
t1.EMPLOYEE_CODE,
nvl(case when substr(t1.calc_month,1,4) =substr(‘20150601‘,1,4)-1
then sum(COLLECT_AMOUNT) over(partition by substr(t1.calc_month,1,4),t1.employee_code) end,0) LAST_PREMIUM, --去年保费
nvl(case when t1.calc_month =substr(‘20150601‘,1,6)
then sum(COLLECT_AMOUNT) over(partition by t1.calc_month,t1.employee_code) end,0) MONTHFEE_ONJOB, --当月保费
nvl(case when substr(t1.calc_month,1,4) =substr(‘20150601‘,1,4) and to_char(trunc(t2.ACCURATE_START_DATE,‘mm‘),‘yyyymm‘) <=t1.calc_month < substr(‘20150701‘,1, 6)
then sum(COLLECT_AMOUNT) over(partition by substr(t1.calc_month,1,4),t1.employee_code) end,0) YEARFEE_ONJOB --当年保费
from (select EMPLOYEE_CODE,calc_month,sum(COLLECT_AMOUNT) COLLECT_AMOUNT
from EPCIS_SAS_COLLECT_CHANNEL
where calc_month < ‘201501‘
group by EMPLOYEE_CODE, calc_month
union all
select EMPLOYEE_CODE,to_char(ACCOUNT_DATE,‘yyyymm‘) calc_month,sum(COLLECT_AMOUNT) COLLECT_AMOUNT
from EPCIS_SAP_COLLECT_CH_EMPDAILY
where ACCOUNT_DATE >= date‘2015-01-1‘
group by EMPLOYEE_CODE, to_char(ACCOUNT_DATE,‘yyyymm‘)) t1,
(SELECT CHN.EMPLOYEE_CODE,
CHN.CHANNEL_TYPE,
MIN(CHN.START_DATE) ACCURATE_START_DATE,
MAX(NVL(CHN.END_DATE, DATE ‘2051-01-01‘)) ACCURATE_END_DATE
FROM (SELECT EMP.EMPLOYEE_CODE,
HIS.CHANNEL_TYPE,
HIS.START_DATE,
HIS.END_DATE,
ROW_NUMBER() OVER(ORDER BY EMP.EMPLOYEE_CODE, HIS.START_DATE) - ROW_NUMBER() OVER(PARTITION BY EMP.EMPLOYEE_CODE, HIS.CHANNEL_TYPE ORDER BY EMP.EMPLOYEE_CODE, HIS.START_DATE) IND
FROM EPCIS_SAS_EMPLOYEE EMP,
EPCIS_SAS_LEVEL_HISTORY HIS
WHERE EMP.HR_CODE IS NOT NULL
and EMP.ENTER_DATE <
to_date(‘20150701‘, ‘yyyymmdd‘)
and NVL(EMP.LEAVE_DATE, SYSDATE) >=
to_date(‘20150601‘, ‘yyyymmdd‘)
AND EMP.EMPLOYEE_CODE = HIS.EMPLOYEE_CODE
and emp.employee_code = ‘2010000001‘) CHN
GROUP BY CHN.EMPLOYEE_CODE, CHANNEL_TYPE, IND
having trunc(MIN(CHN.START_DATE), ‘mm‘) <= to_date(‘20150601‘, ‘yyyymmdd‘)
and MAX(NVL(CHN.END_DATE, DATE ‘2051-01-01‘)) > to_date(‘20150701‘, ‘yyyymmdd‘)) t2
where t1.EMPLOYEE_CODE = t2.EMPLOYEE_CODE
)
group by calc_month,EMPLOYEE_CODE

20150827

标签:

原文地址:http://www.cnblogs.com/zhangwensi/p/4764293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!