Hi Both
Thanks for your help. Only one issue which I can highlight in both of your query that the output i can see is breakdown rather than an aggregate. Sorry, maybe I haven't mentioned in the requirement while I posted the question out.
However I have solved it myself by modifying one of the queries above .This is what worked for me and what i actually wanted. I agree I dont need cte and no pivot is required. However as I started by using cte, I proceeded with this appraoch.
;with stores
as(
select s.store_code,
case when s.SECTOR like 'MatArea%' then 'Mat'
when s.Store_code = 867 then 'Mat_Web'
when s.store_code between 1001 and 1049 then 'MCO'
when s.store_code between 1698 and 1898 then 'Db'
when s.STORE_CODE = 1899 then 'Db_Web'
when s.STORE_CODE<200 then 'TE UK'
when s.store_code between 1051 and 1299 then 'Hb'
when s.store_code between 1301 and 1599 then 'Mas'
when s.store_code between 402 and 498 then 'Ply'
else 'Other' End as 'Channels'
from dbo.dim_store s with (nolock)
--order by cast(s.store_code as int) asc
)
,sales_report as
(
select
s.Channels
,sum(sale_val) as [TY LW Sales]
,sum(sale_val_ly) as [LY LW Sales]
,sum((INV_ALLOCATED_STOCK_VAL + INV_GIT_VAL)) as [TY_TW_SOH_Cost_Curl]
,sum(INV_ALLOCATED_STOCK_VAL_LY + INV_GIT_VAL_LY) as [LY_TW_SOH_Cost_Curl]
,sum(INV_CLOSING_VAL_AT_PP + INV_GIT_VAL + INV_ALLOCATED_STOCK_VAL) as [TY_TW_SOH_Cost]
,sum(INV_CLOSING_VAL_AT_PP_LY + INV_GIT_VAL_LY + INV_ALLOCATED_STOCK_VAL_LY) as [LY_TW_SOH_Cost]
,sum(inv_closing_val_at_hosp + inv_git_val_sp + INV_ALLOCATED_STOCK_VAL_SP) as [TY_TW_SOH_Retail]
,sum(INV_CLOSING_VAL_AT_HOSP_LY + INV_GIT_VAL_SP_LY + INV_ALLOCATED_STOCK_VAL_SP_LY) as [LY_TW_SOH_Retail]
from dbo.AGG_WLY_STR a with(nolock)
join
stores s with (nolock) on a.STORE_CODE = s.store_code
and s.Channels<>'Other'
where a.DATE_KEY = (select max(date_key)-7 from dbo.AGG_WLY_STR with(nolock))
group by Channels
)
select * from sales