title: Postgresql, select value from row as column name without null
body: <p>I have a table:</p>
  
  <pre><code>id;    user_id;    field_name;    value;    timestamp
  1      1           user_status    PENDING   2018-07-31 14:20:28.632838
  2      1           selfie         APPROVED  2018-07-31 14:20:29.632838
  3      2           user_status    PENDING   2018-07-31 14:20:31.632838
  4      1           passport       APPROVED  2018-07-31 14:20:33.632838
  5      3           user_status    PENDING   2018-07-31 14:20:44.632838
  6      2           selfie         REJECTED  2018-07-31 14:20:52.632838
  7      1           user_status    ACTIVATED 2018-07-31 14:20:54.632838
  </code></pre>
  
  <p>I try co select to column from values: latest of user_status, latest of selfie, latest of passport group by userid:</p>
  
  <p>expected table:</p>
  
  <pre><code>user_id;    user_status;    selfie;    passport
  1           ACTIVATED       APPROVED   APPROVED
  2           PENDING         REJECTED   [null]
  3           PENDING         [null]     [null]
  </code></pre>
  
  <p>I used this sql:</p>
  
  <pre><code>select 
      user_id, 
      case when field_name='user_status' then value end as user_status,
      case when field_name='selfie' then value end as selfie,
      case when field_name='passport' then value end passport
  from user_history
  group by user_id, field_name, value
  </code></pre>
  
  <p>but it give a table with null value and not distinct user_id.</p>
  
  <pre><code>user_id;    user_status;    selfie;    passport
  1           PENDING         [null]     [null]
  3           PENDING         [null]     [null]
  1           ACTIVATED       [null]     [null]
  2           [null]          REJECTED   [null]
  1           [null]          APPROVED   [null]
  2           PENDING         [null]     [null]
  1           [null]          [null]     APPROVED
  </code></pre>
  
  <p>Any idea? Thank you.</p>