title: How to SELECT based on value of another SELECT
body: <p>I have a table that looks something like this:</p>
  <pre><code>Name    Year   Value
   A      2000     5
   A      2001     3
   A      2002     7
   A      2003     1
   B      2000     6
   B      2001     1
   B      2002     8
   B      2003     2
  </code></pre>
  <p>The user can query based on a range of years, and it will return the sum of Value grouped by Name, as such (assume queried years are 2000 - 2001):</p>
  <pre><code>Name   SUM(Value)
   A         8
   B         7
  </code></pre>
  <p>Now, I wanted to insert a calculated field that outputs the ratio of the sum of the values of each name for ALL years to the sum of all values. Basically the percentage of all values attributed to A and B, respectively, like:</p>
  <pre><code>Name   SUM(Value)   % Of Total
   A         8            0.484      (16 / 33)
   B         7            0.516      (17 / 33)
  </code></pre>
  <p>Note that even though the user queried only 2000-2001, I want the calculation to use the sum across all years. I've been searching and experimenting for hours and I cannot figure out how. I only know how to sum across the queried years like so:</p>
  <pre><code>SELECT `Name`, SUM(`Value`)/(SELECT SUM(`Value`) FROM `table1`) AS &quot;% of Total&quot;
  FROM `table1`
  WHERE `Year` BETWEEN 2000 AND 2001
  GROUP BY `Name`;
  </code></pre>