SELECT Company , SUM(mastertable.jan1) as January, SUM(mastertable.Feb1) as February, SUM(mastertable.Mar1) as March , SUM(mastertable.Apr1) as April, SUM(mastertable.May1) as May, SUM(mastertable.June1) as June , SUM(mastertable.July1) as July, SUM(mastertable.Aug1) as August, SUM(mastertable.Sept1) as September , SUM(mastertable.Oct1) as October, SUM(mastertable.Nov1) as November, SUM(mastertable.Dec1) as December ,IssYr,MediaGroup, SplitEmpID, AccountID from(select B.company_name as Company,SUM(CASE WHEN IssMth = 1 then (CashOnly) else 0 end) as Jan1 ,SUM(CASE WHEN IssMth = 2 then (CashOnly) else 0 end) as FEB1,SUM(CASE WHEN IssMth = 3 then (CashOnly) else 0 end) as Mar1,SUM(CASE WHEN IssMth = 4 then (CashOnly) else 0 end) as Apr1 ,SUM(CASE WHEN IssMth = 5 then (CashOnly) else 0 end) as May1,SUM(CASE WHEN IssMth = 6 then (CashOnly) else 0 end) as June1 ,SUM(CASE WHEN IssMth = 7 then (CashOnly) else 0 end) as July1 ,SUM(CASE WHEN IssMth = 8 then (CashOnly) else 0 end) as Aug1 ,SUM(CASE WHEN IssMth = 9 then (CashOnly) else 0 end) as Sept1 ,SUM(CASE WHEN IssMth = 10 then (CashOnly) else 0 end) as Oct1,SUM(CASE WHEN IssMth = 11 then (CashOnly) else 0 end) as Nov1 ,SUM(CASE WHEN IssMth = 12 then (CashOnly) else 0 end) as Dec1 ,issyr,MediaGroup, splitempid, accountidfrom [dbo].[FiscalBilling_SalesAdj] Ajoin tbl_master B on A.AccountID = B.master_idwhere A.RevenueType <> 'Sales Adj' group by B.company_name, IssMth,issyr,MediaGroup, splitempid, accountidUNION ALLselect B.company_name as Company,SUM(CASE WHEN IssMth = 1 then (CashOnly) else 0 end) as Jan1 ,SUM(CASE WHEN IssMth = 2 then (CashOnly) else 0 end) as FEB1 ,SUM(CASE WHEN IssMth = 3 then (CashOnly) else 0 end) as Mar1 ,SUM(CASE WHEN IssMth = 4 then (CashOnly) else 0 end) as Apr1 ,SUM(CASE WHEN IssMth = 5 then (CashOnly) else 0 end) as May1 ,SUM(CASE WHEN IssMth = 6 then (CashOnly) else 0 end) as June1 ,SUM(CASE WHEN IssMth = 7 then (CashOnly) else 0 end) as July1 ,SUM(CASE WHEN IssMth = 8 then (CashOnly) else 0 end) as Aug1 ,SUM(CASE WHEN IssMth = 9 then (CashOnly) else 0 end) as Sept1 ,SUM(CASE WHEN IssMth = 10 then (CashOnly) else 0 end) as Oct1,SUM(CASE WHEN IssMth = 11 then (CashOnly) else 0 end) as Nov1 ,SUM(CASE WHEN IssMth = 12 then (CashOnly) else 0 end) as Dec1 ,issyr,MediaGroup, splitempid, companyidfrom [dbo].[RevenuePending_With_Split] Ajoin tbl_master B on CompanyID = B.master_idgroup by MediaGroup,B.company_name,companyid,splitempid,issyr, IssMth) mastertable where mastertable.SplitEmpID is not nullgroup by Company, issyr, mediagroup, splitempid, accountid order by Company

create table #Test( Company varchar(200) ,January int ,February int ,March int ,April int ,May int ,June int ,July int ,August int ,September int ,October int ,November int ,December int ,IssYr Date ,MediaGroup varchar (100) ,SplitEmpID int ,AccountID int)insert into #Test SELECT Company , SUM(mastertable.jan1) as January, SUM(mastertable.Feb1) as February, SUM(mastertable.Mar1) as March , SUM(mastertable.Apr1) as April, SUM(mastertable.May1) as May, SUM(mastertable.June1) as June , SUM(mastertable.July1) as July, SUM(mastertable.Aug1) as August, SUM(mastertable.Sept1) as September , SUM(mastertable.Oct1) as October, SUM(mastertable.Nov1) as November, SUM(mastertable.Dec1) as December ,IssYr,MediaGroup, SplitEmpID, AccountID from(select B.company_name as Company,SUM(CASE WHEN IssMth = 1 then (CashOnly) else 0 end) as Jan1 ,SUM(CASE WHEN IssMth = 2 then (CashOnly) else 0 end) as FEB1,SUM(CASE WHEN IssMth = 3 then (CashOnly) else 0 end) as Mar1,SUM(CASE WHEN IssMth = 4 then (CashOnly) else 0 end) as Apr1 ,SUM(CASE WHEN IssMth = 5 then (CashOnly) else 0 end) as May1,SUM(CASE WHEN IssMth = 6 then (CashOnly) else 0 end) as June1 ,SUM(CASE WHEN IssMth = 7 then (CashOnly) else 0 end) as July1 ,SUM(CASE WHEN IssMth = 8 then (CashOnly) else 0 end) as Aug1 ,SUM(CASE WHEN IssMth = 9 then (CashOnly) else 0 end) as Sept1 ,SUM(CASE WHEN IssMth = 10 then (CashOnly) else 0 end) as Oct1,SUM(CASE WHEN IssMth = 11 then (CashOnly) else 0 end) as Nov1 ,SUM(CASE WHEN IssMth = 12 then (CashOnly) else 0 end) as Dec1 ,issyr,MediaGroup, splitempid, accountidfrom [dbo].[FiscalBilling_SalesAdj] Ajoin tbl_master B on A.AccountID = B.master_idwhere A.RevenueType <> 'Sales Adj' group by B.company_name, IssMth,issyr,MediaGroup, splitempid, accountidUNION ALLselect B.company_name as Company,SUM(CASE WHEN IssMth = 1 then (CashOnly) else 0 end) as Jan1 ,SUM(CASE WHEN IssMth = 2 then (CashOnly) else 0 end) as FEB1 ,SUM(CASE WHEN IssMth = 3 then (CashOnly) else 0 end) as Mar1 ,SUM(CASE WHEN IssMth = 4 then (CashOnly) else 0 end) as Apr1 ,SUM(CASE WHEN IssMth = 5 then (CashOnly) else 0 end) as May1 ,SUM(CASE WHEN IssMth = 6 then (CashOnly) else 0 end) as June1 ,SUM(CASE WHEN IssMth = 7 then (CashOnly) else 0 end) as July1 ,SUM(CASE WHEN IssMth = 8 then (CashOnly) else 0 end) as Aug1 ,SUM(CASE WHEN IssMth = 9 then (CashOnly) else 0 end) as Sept1 ,SUM(CASE WHEN IssMth = 10 then (CashOnly) else 0 end) as Oct1,SUM(CASE WHEN IssMth = 11 then (CashOnly) else 0 end) as Nov1 ,SUM(CASE WHEN IssMth = 12 then (CashOnly) else 0 end) as Dec1 ,issyr,MediaGroup, splitempid, companyidfrom [dbo].[RevenuePending_With_Split] Ajoin tbl_master B on CompanyID = B.master_idgroup by MediaGroup,B.company_name,companyid,splitempid,issyr, IssMth) mastertable where mastertable.SplitEmpID is not nullgroup by Company, issyr, mediagroup, splitempid, accountid order by Companyselect * from #Test

...from(select B.company_name as Company,SUM(CASE WHEN IssMth = 1 then (CashOnly) else 0 end) as Jan1 ,SUM(CASE WHEN IssMth = 2 then (CashOnly) else 0 end) as FEB1,SUM(CASE WHEN IssMth = 3 then (CashOnly) else 0 end) as Mar1,SUM(CASE WHEN IssMth = 4 then (CashOnly) else 0 end) as Apr1 ,SUM(CASE WHEN IssMth = 5 then (CashOnly) else 0 end) as May1,SUM(CASE WHEN IssMth = 6 then (CashOnly) else 0 end) as June1 ,SUM(CASE WHEN IssMth = 7 then (CashOnly) else 0 end) as July1 ,SUM(CASE WHEN IssMth = 8 then (CashOnly) else 0 end) as Aug1 ,SUM(CASE WHEN IssMth = 9 then (CashOnly) else 0 end) as Sept1 ,SUM(CASE WHEN IssMth = 10 then (CashOnly) else 0 end) as Oct1,SUM(CASE WHEN IssMth = 11 then (CashOnly) else 0 end) as Nov1 ,SUM(CASE WHEN IssMth = 12 then (CashOnly) else 0 end) as Dec1 ,issyr,MediaGroup, splitempid, accountidfrom [dbo].[FiscalBilling_SalesAdj] Ajoin tbl_master B on A.AccountID = B.master_idwhere A.RevenueType <> 'Sales Adj' and splitEmpID is not nullgroup by B.company_name, IssMth,issyr,MediaGroup, splitempid, accountidUNION ALLselect B.company_name as Company,SUM(CASE WHEN IssMth = 1 then (CashOnly) else 0 end) as Jan1 ,SUM(CASE WHEN IssMth = 2 then (CashOnly) else 0 end) as FEB1 ,SUM(CASE WHEN IssMth = 3 then (CashOnly) else 0 end) as Mar1 ,SUM(CASE WHEN IssMth = 4 then (CashOnly) else 0 end) as Apr1 ,SUM(CASE WHEN IssMth = 5 then (CashOnly) else 0 end) as May1 ,SUM(CASE WHEN IssMth = 6 then (CashOnly) else 0 end) as June1 ,SUM(CASE WHEN IssMth = 7 then (CashOnly) else 0 end) as July1 ,SUM(CASE WHEN IssMth = 8 then (CashOnly) else 0 end) as Aug1 ,SUM(CASE WHEN IssMth = 9 then (CashOnly) else 0 end) as Sept1 ,SUM(CASE WHEN IssMth = 10 then (CashOnly) else 0 end) as Oct1,SUM(CASE WHEN IssMth = 11 then (CashOnly) else 0 end) as Nov1 ,SUM(CASE WHEN IssMth = 12 then (CashOnly) else 0 end) as Dec1 ,issyr,MediaGroup, splitempid, companyidfrom [dbo].[RevenuePending_With_Split] Ajoin tbl_master B on CompanyID = B.master_idwhere splitEmpID is not nullgroup by B.company_name,MediaGroup,companyid,splitempid,issyr, IssMth) mastertable where mastertable.SplitEmpID is not null...