title: SQL Cursor in dynamic content
body: <p>I need a little help wrapping my head around CURSOR.</p>
  
  <p>I made an aspx report, it was quite simple. For each project I needed to see how many hours went on Research, Development and Internal. That first one was quite simple, I just hardcoded the solution with 3 groups and an equal 3 types of time. Boss thought that was a brilliant report so he wanted it to cover all customers. Unfortunately the next customer had 5 groups and each group had 2-7 different types of time to each.</p>
  
  <p>Now I need a dynamic solution where I can specify the groups (done) and specify which types of time goes into each group (done), last I need to combine those two with the table that record hours spent.</p>
  
  <p>I have a basic cursor where I can list the projects, the type of time and how much time was spent within this group of time.</p>
  
  <p>What I have is:<br>
  Project A -|- Group A -|- 5<br>
  Project B -|- Group A -|- 2<br>
  Project C -|- Group A -|- 10  </p>
  
  <p>Project A -|- Group B -|- 1<br>
  Project B -|- Group B -|- 10<br>
  Project C -|- Group B -|- 2  </p>
  
  <p>Project A -|- Group C -|- 0<br>
  Project B -|- Group C -|- 3<br>
  Project C -|- Group C -|- 7  </p>
  
  <p>Problem is, I need the report to be<br>
  Header____Group A|Group B|Group C|Group N<br>
  Project A -|- 5  -|- 1  -|- 0 -|- x<br>
  Project B -|- 2  -|- 10 -|- 3 -|- y<br>
  Project C -|- 10 -|- 2  -|- 7 -|- z  </p>
  
  <pre>
  DECLARE @iTimeTypeGroupID int
  
  DECLARE cur CURSOR LOCAL READ_ONLY
  FOR 
  SELECT iRefTimeTypeGroupID
  FROM tbl_TimeTypeGrouping
  WHERE iRefCustomerID = @customerID
  
  OPEN cur
  
  FETCH NEXT FROM cur
  INTO @iTimeTypeGroupID
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
      SELECT PT.iRefProjectID
          , PT.iRefTimeTypeID
          , SUM(PT.decNumberOfHours) sumNumberOfHours
      FROM tbl_ProjectTransaction PT
      WHERE iRefTimeTypeID IN (
          SELECT iRefTimeTypeID 
          FROM tbl_TimeTypeGrouping 
          WHERE iRefTimeTypeGroupID = @iTimeTypeGroupID
              AND iRefCustomerID = @customerID)
      GROUP BY PT.iRefProjectID
          , PT.iRefTimeTypeID
  
      FETCH NEXT FROM cur
      INTO @iTimeTypeGroupID
  END
  
  CLOSE cur
  DEALLOCATE cur
  </pre>