title: Best way to flatten/denormalize SQL lookup tables?
body: <p>I have a bunch of tables like this:</p>
  
  <pre><code>Lookup_HealthCheckupRisks
  ------------
  ID  Name
  1   Anemia
  2   Anorexic
  3   Bulemic
  4   Depression
  ...
  122   Syphilis
  
  
  
  PatientRisksOnCheckup
  ------------------
  ID CheckupID RiskID
  1  11        2
  2  11        3
  3  12        1
  4  14        1
  5  14        3
  ...
  </code></pre>
  
  <p>But I need a flattened version, like this:</p>
  
  <pre><code>PatientCheckup
  ------------------
  CheckupID Risk_1 Risk_2 Risk_3 Risk_4 .. Risk_122
  11        0      1      1      0         0
  12        1      0      0      0         0
  13        0      0      0      0         0
  14        1      0      1      0         0
  </code></pre>
  
  <p>I'm clueless how to do this, the best I can think of is to write a temp table, define all 122 columns, and then do <code>If Exists ( SELECT * FROM PatientRisksOnCheckup where RiskID=i and checkupID=j ) INSERT INTO PatientCheckup (1) WHERE CheckupID=j</code> and iterate over<code>i, j...</code> >_&lt;</p>
  
  <p>Writing this query for just one table is doable not the best, but I've need to flatten data like this for another thirty tables of the same size. Er... suggestions please?</p>
  
  <p>I am also curious to know if what I am doing is a common thing to do or not... ?</p>
  
  <p>I am needing to denormalize/flatten the sql data for statistics software.</p>