title: SQL Server 2005 how do I pivot or cross tab data to get my resultset?
body: <p>I am looking to generate a stored proc that will take a CSV string of projectIds and return the following result set.</p>
  
  <p><img src="https://i.stack.imgur.com/ctgFB.gif" alt="alt text"></p>
  
  <p><a href="https://fileshare.egnyte.com/h-s/20110120/ecbe749ca1004f72" rel="nofollow noreferrer">sample data</a></p>
  
  <p>the projectId and name fields come from the project table while the rest of the fields are a pivot of the milestone dates for each project. the milestone fields are for certain milestones of CID in (37, 39, 41, 45, 47, 50, 53, 72, 73, 75, 77)  where I want each respective milestone to show as the name in the excel file (I am guessing to use an alias for each to convert each milestone name to what I want it to show as)</p>
  
  <p>Also note the first milestone field is the BRS (cid=37) start date and the rest of the milestone fields are all end dates including milestone cid 37 and the rest mentioned above.</p>
  
  <p>The dates should represent the projectMilestone dates where available data is had, if there is no  projectMilestone for a particular CID then i need to use the ReleaseSchedule date. i was going to COALESCE(projectmilestone dates, releaseschedual dates) to accomplish this.</p>
  
  <p>the field headers of the milestone names would be such:</p>
  
  <pre><code>CID           NAME in result set (as field headers)
  37            BRS
  39            SRS
  41            SAD
  45            Product Profile Review
  47            SE Integration
  50            IDE
  53            UAT
  72            PE Testing
  73            Code Freeze
  75            Dark Pod
  77            Production
  </code></pre>