Zoals gezegd is dit eigenlijk niet mogelijk, het dichtstbijzijnde dat je kunt krijgen is:
January2014CalculationDate | January2014PLResult | February2014CalculationDate | February2014PLResult
---------------------------+---------------------+-----------------------------+------------------
2014-01-02 | 100 | 2014-02-03 | 300
2014-01-03 | 200 | 2014-02-04 | 400
NULL | NULL | 2014-02-27 | 500
En zelfs dat is niet eenvoudig en ik zou toch adviseren om dergelijke opmaak buiten SQL te hanteren. De eerste stap is om de gegevens per maand te verdelen en vervolgens de datums in elke maand te rangschikken:
SELECT CalculationDate,
PLResult,
CalculationMonth,
DenseRank = DENSE_RANK() OVER(PARTITION BY CalculationMonth ORDER BY CalculationDate)
FROM ( SELECT CalculationDate,
PLResult,
CalculationMonth = DATEADD(MONTH, DATEDIFF(MONTH, 0, CalculationDate), 0)
FROM #PLResultPerDay
) pl;
Dit geeft:
CalculationDate PLResult CalculationMonth DenseRank
2014-01-02 100 2014-01-01 1
2014-01-03 200 2014-01-01 2
2014-02-03 300 2014-02-01 1
2014-02-04 400 2014-02-01 2
2014-02-27 500 2014-02-01 3
U kunt deze gegevens vervolgens draaien:
WITH Data AS
( SELECT CalculationDate,
PLResult,
CalculationMonth,
DenseRank = DENSE_RANK() OVER(PARTITION BY CalculationMonth ORDER BY CalculationDate)
FROM ( SELECT CalculationDate,
PLResult,
CalculationMonth = DATEADD(MONTH, DATEDIFF(MONTH, 0, CalculationDate), 0)
FROM #PLResultPerDay
) pl
)
SELECT Jan2014CalcDate = MIN(CASE WHEN CalculationMonth = '20140101' THEN CalculationDate END),
Jan2014Result = SUM(CASE WHEN CalculationMonth = '20140101' THEN PLResult END),
Feb2014CalcDate = MIN(CASE WHEN CalculationMonth = '20140201' THEN CalculationDate END),
Feb2014Result = SUM(CASE WHEN CalculationMonth = '20140201' THEN PLResult END)
FROM Data
GROUP BY DenseRank
ORDER BY DenseRank;
Dit geeft:
Jan2014CalcDate Jan2014Result Feb2014CalcDate Feb2014Result
2014-01-02 100 2014-02-03 300
2014-01-03 200 2014-02-04 400
NULL NULL 2014-02-27 500
Aangezien u een dynamisch aantal maanden heeft, moet u de bovenstaande verklaring dynamisch opbouwen en SP_EXECUTESQL
gebruiken om het uit te voeren:
DECLARE @SQL NVARCHAR(MAX) = '';
WITH Months AS
( SELECT M,
ColName = DATENAME(MONTH, M) + DATENAME(YEAR, M),
CharFormat = CONVERT(VARCHAR(8), M, 112)
FROM ( SELECT DISTINCT M = DATEADD(MONTH, DATEDIFF(MONTH, 0, CalculationDate), 0)
FROM #PLResultPerDay
) m
)
SELECT @SQL = 'WITH Data AS
( SELECT CalculationDate,
PLResult,
CalculationMonth,
DenseRank = DENSE_RANK() OVER(PARTITION BY CalculationMonth ORDER BY CalculationDate)
FROM ( SELECT CalculationDate,
PLResult,
CalculationMonth = DATEADD(MONTH, DATEDIFF(MONTH, 0, CalculationDate), 0)
FROM #PLResultPerDay
) pl
)
SELECT ' +
STUFF(( SELECT ', ' + ColName + 'CalculationDate = MIN(CASE WHEN CalculationMonth = ''' + CharFormat + ''' THEN CalculationDate END), ' +
ColName + 'PLResult = SUM(CASE WHEN CalculationMonth = ''' + CharFormat + ''' THEN PLResult END)'
FROM Months
ORDER BY M
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 2, '') +
'FROM Data
GROUP BY DenseRank
ORDER BY DenseRank;';
EXECUTE SP_EXECUTESQL @SQL;
Let op, ik raad deze techniek nog steeds af en vind dat SQL moet worden overgelaten aan het opslaan/ophalen van gegevens, en de presentatielaag voor het formatteren ervan