title: SQL query to calculate frequency in Hive
body: <p>I have a table <code>tab</code> in Hive that looks like this:  </p>
  
  <pre><code>word | occurrences  
  ---- | -----------  
  by   | 10
  hi   | 1
  same | 3
  love | 6
  </code></pre>
  
  <p>I would like to calculate and display the frequencies of the words (occurrences divided by the sum of the entire column) using Hive query. For example, the frequency of the word 'by' is 10/(10+1+3+6) = 0.5.</p>
  
  <p>I tried this:</p>
  
  <pre><code>SELECT word, occurrences, occurrences/SUM(occurrences) AS frequency
  FROM tab
  GROUP BY word, occurrences
  ORDER BY frequency;
  </code></pre>
  
  <p>But it gives this:</p>
  
  <pre><code>word | occurrences | frequency
  ---- | ----------- | ---------
  by   | 10          | 1
  hi   | 1           | 1
  same | 3           | 1
  love | 6           | 1
  </code></pre>
  
  <p>I'm not sure what I'm doing wrong. I'm not very good SQL. Thanks in advance.</p>