declare @cols varchar(max)
declare @strSQL varchar(max)
set @cols = stuff((select distinct '], [' + cast(representativecolumn as varchar) from Orders for xml path('')), 1, 2, '') + ']'
set @strSQL = 'select week, ' + @cols + ' from
(
select dateadd(d, 7 - datepart(dw, b.Odt_dtmvdatum), b.Odt_dtmvdatum) as week,
a.Representativecolumn,
a.Ord_bdrTotaal
from Orders a
INNER JOIN [Orderdetails] b ON a.Ord_ID = b.Odt_Ord_ID
WHERE YEAR(b.Odt_dtmvdatum) = 2010
) o
pivot (sum(Ord_bdrTotaal) for Representativecolumn in ( ' + @cols + '))p
'
exec(@strSQL)
|