title: How do I transpose rows into columns dynamically in SQL?
body: <blockquote>
    <p>To give you a brief summary—I would like to:<br/><br/> 1. Join two tables both containing Address Book information, <br/><br/> 2. Concatenate the ‘phone number area code’ and ‘phone number’ (because they are stored in separate columns in our database) <br/><br/> 3. If the ‘Address Book #’ from Column 1 is repeated—have the information appear on the same row (see below for example). </p>
  </blockquote>
  
  <p><br/> Here are the tables and fields I am using:</p>
  
  <p><strong>Fields in Table 1 (ABC_F0115):</strong>
  <br/>
  ‘Address Book#’ <br/>
  ‘Phone Type’<br/>
  ‘Phone Area Code’<br/>
  “Phone Number’<br/></p>
  
  <p><strong>Fields in Table 2 (ABC_F0101):</strong>
  <br/>
  ‘Address Book#’<br/>
  ‘Address Book Type’<br/>
  <br/></p>
  
  <p>My current SQL is: <br/></p>
  
  <pre><code>SELECT WPAN8 'Address Book#', WPPHTP 'Phone Type', WPAR1 'Phone Area Code', WPPH1 'Phone Number', ABAT1 'Address Book concat('(',LTRIM(RTRIM(WPAR1)),')', ' ', WPPH1) AS 'Full Phone Number' FROM ABC.F0101 JOIN ABC.F0115 ON WPAN8=ABAN8 WHERE ABAT1='AR';
  </code></pre>
  
  <p><br/><br/>
  It returns a table similar to the one below: 
  <br/></p>
  
  <hr>
  
  <p>|AddressBook#| PhoneType | PhoneAreaCode | Phone Number  |<br/>
  |___________ _   _________ _ _____________   _  ___________|<br/>
  |2160________| Cell______| 000__________| 000-0000_____ |
  <br/>
  |2160________| Fax______ | 111__________| 111-1111______ |
  <br/>
  |2161________| Cell______| 222__________| 222-2222_____ |
  <br/>
  |2161________| Fax______ | 333__________| 333-3333_____ |
  <br/>
  |2162________| Home____ | 444__________| 444-4444_____ |</p>
  
  <hr>
  
  <p>However, I would like the returned table to look like:<br/><br/></p>
  
  <hr>
  
  <p>|AddressBook#| PhoneType1 | Phone#1 | PhoneType2  | Phone#2 | PhoneType3 | Phone#3 |<br/>
  |___________ _   _________ _ _______   _  _________ _ _________ _ _______ _ _______ |<br/>
  |2160________| Cell_______| 000-0000| Fax _______ |111-1111_|___________|________|
  <br/>
  |2161________| Cell_______| 222-2222| Fax _______ |333-3333 |___________|________|
  <br/>
  |2163________| Home_____| 444-4444| Fax _______ |333-3333 |___________|________ |
  <br/></p>
  
  <hr>
  
  <p>I know I have to transpose my rows into columns dynamically... but I can't quite figure out my SQL. Any suggestions?</p>