title: SQL query. Need to GROUP BY the combination of values in two columns
body: <p>So I have two tables. One with amount of shares that people have:</p>
  
  <pre><code>Person_ID ||  Shares
  1         ||  100
  2         ||  150
  3         ||  315
  4         ||  75
  5         ||  45
  ...
  </code></pre>
  
  <p>and another with info whether a person is present (there are two columns specifying that) and what group does he belongs to:</p>
  
  <pre><code>Person_id || barcode_presence || manual_presence || group_id
  1         || NULL             || 1               || NULL
  2         || 1                || NULL            || 1
  3         || 1                || 0               || 2
  4         || 0                || NULL            || 1
  5         || 1                || 0               || 1
  ...
  </code></pre>
  
  <p>I need to find out the sum of all present and sum of all absent shares in every single group (NULL group is also a group).</p>
  
  <p>The thing I cannot figure out is that whether the person is present or absent is figured out by the combination of two columns (barcode_presence and manual_presence):</p>
  
  <p>if manual_presence = 1   --> present</p>
  
  <p>else if (manual_presence = NULL and barcode_presence = 1)     --> present</p>
  
  <p>everything else is absent.</p>
  
  <p>Thats what i've got so far.</p>
  
  <pre><code>SELECT  presence_list.Person_id, presence_list.person_name, 
  presence_list.barcode_presence, presence_list.manual_presence,
  presence_list.group_id, people_data.Share 
  , sum(people_data.Share)
  
  FROM presence_list
  
  INNER JOIN people_data
  ON presence_list.Person_id = people_data.Person_ID
  
  WHERE meeting_id = 1 AND voting_id = 0 
  GROUP BY presence_list.group_id,  
  barcode_presence,
  manual_presence
  ORDER BY presence_list.group_id
  ;
  </code></pre>
  
  <p>But obviously that does not give me the desired result. it groups by every possible combination of group_id, barcode_presence and manual_presence like following:</p>
  
  <pre><code>Person_id || barcode_presence || manual_presence || group_id || share || sum(shares)
  1         || NULL             || 1               || NULL     || 100   || 100
  2         || 1                || NULL            || 1        || 150   || 150
  4         || 0                || NULL            || 1        || 75    || 75
  5         || 1                || 0               || 1        || 45    || 45
  3         || 1                || 0               || 2        || 315   || 315
  </code></pre>
  
  <p>So i'm looking for a way to (in this case) connect person with id 4 and 5, because they are both absent and they are from the same group, like this: </p>
  
  <pre><code>Person_id || barcode_presence || manual_presence || group_id || share || sum(shares)
  1         || NULL             || 1               || NULL     || 100   || 100
  2         || 1                || NULL            || 1        || 150   || 150
  5         || 1                || 0               || 1        || 45    || **120**
  3         || 1                || 0               || 2        || 315   || 315
  </code></pre>
  
  <p>I've tried using <code>HAVING</code> clause but I didn't found a way for it to handle complex expressions like <code>manual_presence = 1 OR (manual_presence is null AND barcode_presence = 1)</code></p>
  
  <p>Any ideas or suggestions would be appreciated! thanks beforehand!</p>