Hi
I need a wee bit help or suggestion with a crosstab I hope to create.
Here's the code so far:
select 'dbr.crosstab','[yr]';
select 'dbr.crosstab.header', '[metric]';
select 'dbr.crosstab.header.col','Net Sales','% VA','% GM';
select 'dbr.lockcolumns','cust';
select 'dbr.colstyle','val','[width:65px;]%0.0f';
select 'dbr.hidecolumn','cid';
select 'dbr.sum','val';
select a.[Customer] as "Customer[cust]" ,a.[CustID] as "CustID[cid]" ,'Net Sales' as "[metric]" ,b.[AccYear] as "Financial Year Metrics[yr]" ,sum(a.[Net]) as "[val]" FROM [pharma].[dbo].[vw_mon_inv_sales] a cross apply (select [AccYear], [AccYearDesc] from [pharma].[dbo].[tblAccountYears_v2] b where a.InvMon between b.[AccYearSDate] and b.[AccYearEDate]) b where a.[InvMon]>='2017-07-01' and a.Comp=1 and len(a.FPO)=5 group by a.[CustID],a.[Customer],b.[AccYear] union all select a.[Customer] ,a.[CustID] ,'% VA' ,b.[AccYear] ,(sum(a.[Net]-a.[mcosts])/sum(a.[Net]))*100 FROM [pharma].[dbo].[vw_mon_inv_sales] a cross apply (select [AccYear], [AccYearDesc] from [pharma].[dbo].[tblAccountYears_v2] b where a.InvMon between b.[AccYearSDate] and b.[AccYearEDate]) b where a.[InvMon]>='2017-07-01' and a.Comp=1 and len(a.FPO)=5 group by a.[CustID],a.[Customer],b.[AccYear] union all select a.[Customer] ,a.[CustID] ,'% GM' ,b.[AccYear] ,(sum(a.[Net]-a.[mcosts]-a.[Hours]*@labcost)/sum(a.[Net]))*100 FROM [pharma].[dbo].[vw_mon_inv_sales] a cross apply (select [AccYear], [AccYearDesc] from [pharma].[dbo].[tblAccountYears_v2] b where a.InvMon between b.[AccYearSDate] and b.[AccYearEDate]) b where a.[InvMon]>='2017-07-01' and a.Comp=1 and len(a.FPO)=5 group by a.[CustID],a.[Customer],b.[AccYear] order by 1 ;
This crosstab is working fine except for two things:
The % VA and % GM summary values are sum of their respective columns - how do I get them as %?
Is there a way to style the & VA and % GM independently? I'd like to show those columns with 1 decimal place, from the Net Sales - they all use the alias [val].
Is a crosstab the best way to do this?
Thanks
Jake