title: Combine selected data into same row based on column id (order)
body: <p>I have a combination of tables that are used to described location flows of people. The main 'travel_flows' tables has a structure as seen in the first table below. The order of the flows is described by the 'order_id' column.</p>
  
  <pre>
  +-----------+------------+---------+-------------+----------+
  | travel_id | purpose_id | name_id | location_id | order_id |
  +-----------+------------+---------+-------------+----------+
  |       434 |         23 |      55 |          85 |        1 |
  |       212 |         43 |      55 |          45 |        2 |
  |       411 |         41 |      55 |          17 |        3 |
  |       148 |         23 |      32 |          32 |        1 |
  |       153 |         11 |      32 |          19 |        2 |
  +-----------+------------+---------+-------------+----------+
  </pre>
  
  <p>What I am trying to achieve using PostgreSQL 9.6 is to group the returned rows by 'name_id' in a binary origin(from)/destination(to) format based on 'location_id' and 'order_id' value, similar to the following table:</p>
  
  <pre>
  +-----------------+--------------+------------------+---------------+------------+----------------+
  | from_purpose_id | from_name_id | from_location_id | to_purpose_id | to_name_id | to_location_id |
  +-----------------+--------------+------------------+---------------+------------+----------------+
  |              23 |           55 |               85 |            43 |         55 |             45 |
  |              43 |           55 |               45 |            41 |         55 |             17 |
  |              23 |           32 |               32 |            11 |         32 |             19 |
  +-----------------+--------------+------------------+---------------+------------+----------------+
  </pre>
  
  <p>Is there any way to achieve this with a select statement?</p>