|
--only execute only part 1 first, then only part 2
--Part 1
select
L01, L03, A05 , case when A_ACTIVE=1 and A17=4 and TOTEXP>365 then TOTEXP-ROUND(TOTEXP/3,0) else TOTEXP end as TOTEXP , case when A_ACTIVE=1 and A17=4 and TOTEXP>365 then TOTACT-ROUND(TOTACT/3,0) else TOTACT end as TOTACT
into #totact_totexp
from ( select
L01, L03, A05 , datediff(dd,A27,A31)+1 as TOTACT , datediff(dd,A27,A28)+1 as TOTEXP , A_ACTIVE, A17
from
MISVS001.LSC_MI_db_pub.dbo.ILR0910_L01_AIMS )T
--Part 2
select
L01, L03, A05 , case when max(A_INYR_EXPECTED_GLH_qry)>max(A32) then max(A32) when max(A17)=6 or max(A_ACTIVE)<>1 then 0 else max(A_INYR_EXPECTED_GLH_qry) end as A_INYR_EXPECTED_GLH_q
from ( select
A.L01, A.L03, A.A05, A_ACTIVE, A32, A17 , case when A_ACTIVE=1 and A34<>2 and A27<'1 august 2009' and A28>='1 august 2009' and A28<='31 july 2010' then (datediff(dd,'1 august 2009',A28)/(TOTEXP*1.0))*A32 when A_ACTIVE=1 and A34<>2 and A27<'1 august 2009' and (A28<'1 august 2009' or A28>'31 july 2010') then (datediff(dd,'1 august 2009','31 july 2010')/(TOTEXP*1.0))*A32 when A_ACTIVE=1 and A34<>2 and A27>='1 august 2009' and A28>='1 august 2009' and A28<='31 july 2010' then (datediff(dd,A27,A28)/(TOTEXP*1.0))*A32 when A_ACTIVE=1 and A34<>2 and A27>='1 august 2009' and (A28<'1 august 2009' or A28>'31 july 2010') then (datediff(dd,A27,'31 july 2010')/(TOTEXP*1.0))*A32 when A_ACTIVE=1 and A34=2 and A27<'1 august 2009' and A28>='1 august 2009' and A28<='31 july 2010' then ((datediff(dd,'1 august 2009',A28)+1)/(TOTACT*1.0))*A32 when A_ACTIVE=1 and A34=2 and A27<'1 august 2009' and (A28<'1 august 2009' or A28>'31 july 2010') then ((datediff(dd,'1 august 2009','31 july 2010')+1)/(TOTACT*1.0))*A32 when A_ACTIVE=1 and A34=2 and A27>='1 august 2009' and A28>='1 august 2009' and A28<='31 july 2010' then ((datediff(dd,A27,A28)+1)/(TOTACT*1.0))*A32 when A_ACTIVE=1 and A34=2 and A27>='1 august 2009' and (A28<'1 august 2009' or A28>'31 july 2010') then ((datediff(dd,A27,'31 july 2010')+1)/(TOTACT*1.0))*A32 end as A_INYR_EXPECTED_GLH_qry
from MISVS001.LSC_MI_db_pub.dbo.ILR0910_L01_AIMS A
join
#totact_totexp B
on A.L01=B.L01 and A.L03=B.L03 and A.A05=B.A05
)T
group by
L01, L03, A05
order by
L01, L03, A05 |