I got the code below from ralmada and it works fine producing output as illustrated in TABLE A below.
I would now like to modify the code so that the returned column names for the Dates are appended with the text 'Hardware' as shown in TABLE B
I have concatenated 'Hardware' in the variable @colshw
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8)+' Hardware'
which compiles fine and runs but the results are as TABLE B, '0' for all cell entries
any help appreciated...thanks
/* pivot for hardware */
declare @strSQLhw varchar(max);
declare @colshw varchar(max);
;with CTE as (
select dateadd(m, datediff(m, 0, @start_date_range), 0) as st,
dateadd(m, datediff(m, 0, @end_date_range), 0) as et
select dateadd(m, 1, st),
where dateadd(m, 1, st) <= et
select @colshw = stuff((select '],[' + right(convert(varchar, st, 106), 8)
from CTE for xml path('')), 1, 2, '') + ']'
set @strSQLhw = '
select server_name, ' + @colshw + ' from (
right(convert(varchar, dateadd(m, datediff(m, 0, [resolved_date]), 0), 106), 8)
as eom, PROD_CAT1
where [resolved_date] >= ''' +
112) + ''' and [resolved_date] < ''' +convert(varchar, @end_date_range+1,112) + ''') o
pivot (count(PROD_CAT1) for eom in (' + @colshw +' )) as p '