views:

71

answers:

0

I am having a really difficult time getting the result I need out of the following query.

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 = '138'
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)))

This Returns columns MTTR, OTR, REPEAT, CHRONIC, TOTAL, YearMonth with the results in the rows.

What I am trying to pull of is to have each of the retrieved YearMonth Results displayed as a colum and then the rows should be MTTR, OTR, REPEATE, CHRONIC, TOTAL

Now from what I've read. Since the YearMonth Values are going to be dynamically generated I either have to use Dynamic SQL or Case statements. The query will be ran from a .net application and I would prefer not to have to store the query on the database itself.

UPDATE

Alright. This is the "static" version of what I need to pull off. The month range is going to be user selectable, so the columns will be dynamic.

with cte (YearMonth, MTTR, OTR, REPEAT, CHRONIC, TOTAL)
      as
      (
      SELECT CASE WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '01' THEN 'Jan' 
                            WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '02' THEN 'Feb ' + RIGHT(CAST(YEAR([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(4)), 2)
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '03' THEN 'Mar'  
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '04' THEN 'Apr'  
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '05' THEN 'May'
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '06' THEN 'Jun'
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '07' THEN 'Jul'
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '08' THEN 'Aug'
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '09' THEN 'Sep'
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '10' THEN 'Oct'
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '11' THEN 'Nov'
       WHEN Right('00' + CAST(MONTH([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(2)), 2) = '12' THEN 'Dec' END AS [YearMonth],
      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
      FROM TRT_remedy_tickets 
      WHERE COMPID = '138' 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 '2009-01' AND '2009-12'
      Group By TRT_remedy_tickets.DATE_RESOLVED_FOR_CLOSURE
      )
      ,cte2 (YearMonth, health, metric)
      as(
       select unpvt.YearMonth, unpvt.health, unpvt.metric
       from
       (
        select YearMonth, MTTR, OTR, REPEAT, CHRONIC, TOTAL
        from cte
       ) as x
       unpivot
       (
        health for metric in (x.MTTR, x.OTR, x.REPEAT, x.CHRONIC, x.TOTAL)
       ) as unpvt
      )

      select pvt.metric, pvt.Jan, pvt.[Feb 09], pvt.Mar, pvt.Apr, pvt.May, pvt.Jun, pvt.Jul, pvt.Aug, pvt.Sep, pvt.Oct, pvt.Nov, pvt.Dec
      from
      (
       select YearMonth, health, metric
       from cte2
      ) as x
      pivot
      (
       sum(x.health) for x.YearMonth in (Jan, [Feb 09], Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
      )as pvt

Since I do not know of a better solution what I plan to do at this point is create the SelectCommand in VB within the code behind. Such that when the user selects a date range that triggers the sub to build the selectcommand and save it to a public string. Then set within the pageload sub HealthScoreDS.SelectCommand = PublicString