I am trying to write a query in my .net app and I'm having trouble pivoting the table since the columns are generated dynamically (based on a month range selected by the user)
SELECT SUM(HEALTH_MTTR) AS MTTR, SUM(HEALTH_OTR) AS OTR, SUM(HEALTH_REPEAT) AS REPEAT, SUM(HEALTH_CHRONIC) AS CHRONIC, SUM(HEALTH_TOTAL) AS TOTAL, (CAST(CAST(YEAR([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(4)) + '-' + Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) AS VARCHAR(7))) As YearMonth
FROM TRT_remedy_tickets
WHERE COMPID = @COMPID AND (CAST(CAST(YEAR([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(4)) + '-' + Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) AS VARCHAR(7))) BETWEEN @START AND @END
Group By (CAST(CAST(YEAR([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(4)) + '-' + Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) AS VARCHAR(7)))
Generates a table like so:
MTTR OTR REPEAT CHRONIC TOTAL YearMonth
x x x x x 2009-06
x x x x x 2009-07
x x x x x 2009-08
And I need the table to be displayed like so
TYPE 2009-06 2009-07 2009-08 2009-09
MTTR x x x x
OTR x x x x
REP x x x x
CHR x x x x
TOT x x x x