select fsl_dstId from tbl_cdr_distributors where cdr_recordid in () and ownerid = and fsl_tablecode = with data as ( select oa.agreementOwnerName, oa.cho_id, sum( oa.#_uomColumn# ) #_uomColumn#, case when oa.purchasingMonth >= then 'P1' else 'P0' end as periodCode from tbl_DW_OperatorAnalyzer#_tableSuffix# oa with (nolock) where oa.ownerId = and oa.ownerType = and oa.dstId in () and oa.agreementOwnerChoId = oa.cho_id and oa.requestId is not null and ( oa.purchasingMonth between and or oa.purchasingMonth between and ) group by oa.agreementOwnerName,oa.cho_id, oa.purchasingMonth ) select ROW_NUMBER() OVER(ORDER BY P1 desc) AS rowNumber, agreementOwnerName,cho_id, coalesce( P1, 0.0 ) as P1, coalesce( P0, 0.0 ) as P0, coalesce( P1, 0.0 ) - coalesce( P0, 0.0 ) change, case when coalesce( P0, 0.0 ) != 0 then ( coalesce( P1, 0.0 ) - coalesce( P0, 0.0 ) ) / coalesce( P0, 0.0 ) * 100.0 else null end as changePct, case when coalesce( p1, 0.0 ) != 0 then round( coalesce( p1, 0.0 ) / sum( coalesce( p1, 0.0 )) over() * 100.0, 1) else 0.00 end as pctOfTotal from ( select * from data ) as ps pivot ( sum( #_uomColumn# ) for periodCode in ( P1, P0 ) ) as pvt