title: Return unique distinct value for group by dynamic fields based on value
body: <p>I have the following SQL query:</p>
  
  <pre><code>SELECT DISTINCT
      username,
      (CASE WHEN role_id = 1 THEN SUM(user_roles.hours) ELSE NULL END) AS `Admin`,
      (CASE WHEN role_id = 2 THEN SUM(user_roles.hours) ELSE NULL END) AS `Subscriber`
  FROM user_roles
  LEFT JOIN users ON users.id = user_roles.user_id;
  </code></pre>
  
  <p>The purpose of this query is to display on each rows the usernames and for each username the number of hours for Admins, Subscribers and so on.</p>
  
  <p>This query produces the following results:</p>
  
  <pre><code>username    Admin    Subscriber
  abc         10       0
  abc         0        5
  def         0        7
  def         4        0
  </code></pre>
  
  <p>Each username should have a unique value that falls either under Admin or Subscriber (no users can have both)</p>
  
  <p>I would like the output to be instead:</p>
  
  <pre><code>username    Admin    Subscriber
  abc         10       5
  def         4        7
  </code></pre>
  
  <p>Where there are no columns that show zeros and to have only one row result per usernames.</p>
  
  <p>I hope it's clear what I'm trying to accomplish here.</p>
  
  <p>Thanks for any help</p>