I was just playing with dynamic pivoting the other day! Here is a fully functional testing script I created while doing it. Although it's based on a CROSS JOIN example, it should contain some useful insight. I hope you can glean some ideas about how to get your desired results.
IF OBJECT_ID(N'dbo.Strikes','U') IS NOT NULL
DROP TABLE dbo.Strikes;
CREATE TABLE dbo.Strikes(
Dimension nvarchar(10) NOT NULL PRIMARY KEY ,
DimVal smallint DEFAULT 0);
INSERT INTO dbo.Strikes (Dimension, DimVal)
VALUES (N'Fire',5), (N'Water',4), (N'Earth',3), (N'Air',2), (N'Spirit',1), (N'George',0);
/* Dynamic Pivot Exmple */
--declare variables and cursor
DECLARE @PvtClm varchar(MAX)
DECLARE @PTmp AS varchar(10)
DECLARE PvtCsr CURSOR FOR
SELECT DISTINCT Dimension
FROM dbo.Strikes
ORDER BY Dimension
--open and fill pivot column string
OPEN PvtCsr
SET @PvtClm = N'['
FETCH NEXT FROM PvtCsr INTO @PTmp
WHILE @@FETCH_STATUS = 0
BEGIN
SET @PvtClm = @PvtClm + RTRIM(@PTmp) + N'], ['
FETCH NEXT FROM PvtCsr INTO @PTmp
END
--kill cursor and finish pivot string
CLOSE PvtCsr
DEALLOCATE PvtCsr
--strip bogus end characters to finish pivot string
SET @PvtClm = LEFT(@PvtClm,(LEN(@PvtClm)-3));
--PRINT @PvtClm
--set the dynamic pivot sql string
DECLARE @DynPivot nvarchar(MAX)
SET @DynPivot = N'
WITH CrJoin AS (
SELECT S1.Dimension AS SourceElement,
S2.Dimension AS TargetElement,
S1.DimVal - S2.DimVal AS Modifier
FROM dbo.Strikes AS S1
CROSS JOIN dbo.Strikes AS S2)
SELECT SourceElement, ' + @PvtClm + '
FROM CrJoin
PIVOT(
MAX(CrJoin.Modifier)
FOR CrJoin.TargetElement IN(' + @PvtClm + ')
) AS PvT;'
--PRINT @DynPivot
--run the pivot
EXEC sp_executesql @stmt = @DynPivot;
IF OBJECT_ID(N'dbo.Strikes','U') IS NOT NULL
DROP TABLE dbo.Strikes;