select T2. el1_code , T1.leaforder, T1.l1order, sum(T3.full_value) AS FullValue , sum(case when T4.code = 'ELM' then T3."full_value" else 0 end ) ELM , sum(case when T4.code = 'PFC' then T3."full_value" else 0 end ) PFC , sum(case when T4.code = 'OPM' then T3."full_value" else 0 end ) OPM , sum(case when T4.code = 'OSY' then T3."full_value" else 0 end ) OSY , sum(case when T4.code = 'ZTL' then T3."full_value" else 0 end ) ZTL , sum(case when T4.code = 'PRO' then T3."full_value" else 0 end ) PRO , sum(case when T4.code = 'OID' then T3."full_value" else 0 end ) OID , sum(case when T4.code = 'OPI' then T3."full_value" else 0 end ) OPI into #Test from oas_agmlist T5, oas_himlist T1, oas_balance T3, oas_el1_element T2, oas_company T4 where T5.code = T1.grpcode and (T5.cmpincfrom = T3.cmpcode or T5.cmpincfrom = '*') and T5.el1incfrom <= T3.el1 and T5.el1incto >= T3.el1 and (T5.el2incfrom <= T3.el2 and T5.el2incto >= T3.el2 or T5.el2incfrom = '*') and (T5.el3incfrom <= T3.el3 and T5.el3incto >= T3.el3 or T5.el3incfrom = '*') and T3.cmpcode = T2.el1_cmpcode and T3.el1 = T2.el1_code and T4.code = T3.cmpcode and T1.code = '10QSCHEDULE' and T1.l1name = 'OTHER ASSETS' and (T3.yr between T4.yrmin and 2011 or T3.yr =@Year and T3.period <=@Period) and T3."balcode" = 'ACTUALS' and T3.repbasis = 0 and T3.curcode = 'USD' group by T1.l1order, T1.leaforder, T2.el1_code select T2. el1_code , T1. leaforder , T1. l1order , case when T1. l1ftrhide = 1 then 1 else 0 end L1FTRHIDE , T4."yrmin" , T4."code" , T3."full_value" , T3."repbasis" , T3."curcode" , T3."balcode" , T3."period" , T3."yr" , T2."el1_name" , T1."leafftrtxt" , T1."leafhdrtxt" , T1."leafname" , T1."l1ftrtxt" , T1."l1hdrtxt" , T1."l1name" , T1."code" AS CodeC into #Test1 from "oas_agmlist" T5, "oas_himlist" T1, "dbo"."oas_balance" T3, "dbo"."oas_el1_element" T2, "dbo"."oas_company" T4 where T5."code" = T1."grpcode" and (T5."cmpincfrom" = T3."cmpcode" or T5."cmpincfrom" = '*') and T5."el1incfrom" <= T3."el1" and T5."el1incto" >= T3."el1" and (T5."el2incfrom" <= T3."el2" and T5."el2incto" >= T3."el2" or T5."el2incfrom" = '*') and (T5."el3incfrom" <= T3."el3" and T5."el3incto" >= T3."el3" or T5."el3incfrom" = '*') and T3."cmpcode" = T2."el1_cmpcode" and T3."el1" = T2."el1_code" and T4."code" = T3."cmpcode" and T1."code" = '10QSCHEDULE' and T1."l1name" = 'OTHER ASSETS' and (T3."yr" between T4."yrmin" and 2011 or T3."yr" =@Year and T3."period" <=@Period) and T3."balcode" = 'ACTUALS' and T3."repbasis" = 0 and T3."curcode" = 'USD' SELECT d.fullvalue, d.ELM , d.PFC , d.OPM , d.OSY , d.ZTL , d.PRO ,
FM