title: How to use row values as columns in a SELECT statement in PostgreSQL?
body: <p>I've got a table structure with these three tables in my PostgreSQL 9.2.8 database:</p>
  
  <p><a href="https://i.stack.imgur.com/UhAuM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UhAuM.png" alt="enter image description here"></a></p>
  
  <p>I'm trying to figure out how to select an <code>orders</code> row and on the SAME row add some of the columns from <code>order_points</code> and <code>points</code>.  </p>
  
  <p>You can think of the <code>points</code> table like a list of all items that a person could purchase, where it's known internally by the <code>abbrev</code> and it costs <code>amount</code>.</p>
  
  <p>The <code>order_points</code> table is each item purchased, so <code>points.id == order_points.points_id</code>, and <code>amount</code> would be similar to saying they bought 5 candy bars.  It links to an orders via <code>order_points.order_id == orders.id</code></p>
  
  <p>When I select an order, I want to have a column for each <code>abbrev</code> that exists as well as the <code>amount</code> from the <code>order_points</code> table.</p>
  
  <p>So if <code>points</code> has this:</p>
  
  <pre><code> id | name      | abbrev | amount
   --------------------------------
    1 | Snickers  | sn     | 1.34
    2 | Milky Way | mw     | 1.73
    3 | Coffee    | cf     | 10.12
  </code></pre>
  
  <p>and <code>order_points</code> has this:</p>
  
  <pre><code> id | order_id | points_id | amount
   ----------------------------------
    1 |        1 |         1 |     10
    2 |        1 |         3 |      1
  </code></pre>
  
  <p>Then when I get my row I want all the columns from the order, plus three extra columns.  I don't want to list every single column shown in orders above but basically assume I just wanted 4 of them, plus all the <code>points</code> stuff I'd end up with this as a row of output:</p>
  
  <pre><code> id | created    | due        | name  | sn | mw | cf
   ------------------------------------------------
    1 | 2018-04-21 | 2018-05-01 | Fooey | 10 |  0 |  1
  </code></pre>
  
  <p>I've no idea how to dynamically add a column with a name (the <code>abbrev</code>) from a table lookup.</p>