title: Postgresql conditional sum
body: <p>I have a table with the following data for sales and inventory (oh):</p>
  
  <pre><code>category sales  oh  item_num
  Clothes  12     10  1
  Clothes  11     10  1
  Clothes  10     10  1
  Clothes  5      10  1
  Clothes  8      10  1
  Clothes  4      10  1
  Clothes  23     10  2
  Clothes  5      10  2
  Clothes  20     10  2
  Clothes  5      10  2
  Clothes  13     10  2
  Clothes  9      10  2
  Food     6      25  3
  Food     8      25  3
  Food     7      25  3
  Food     14     25  3
  </code></pre>
  
  <p>I am trying to query this table to get a sum of both the sales and oh columns by category:</p>
  
  <pre><code>SELECT category, SUM(sales) AS sales, SUM(oh) AS oh
  FROM data
  GROUP BY category
  </code></pre>
  
  <p>However, the problem is I need the <code>SUM(oh)</code> to only sum distinct items but the <code>SUM(sales)</code> to sum all the values. So the result should be:</p>
  
  <pre><code>category   sales   oh
  Clothes     125    20
  Food        35     25
  </code></pre>
  
  <p>I tried <code>SUM(DISTINCT oh)</code>, but that only works for distinct oh values not distinct items. I really need something like <code>SUM(DISTINCT(item_num) oh)</code>.</p>
  
  <p>I experimented with various window functions, but could not come up with a solution. Does anyone know how to return this kind of sum on a unique key?</p>