title: Between clause from recordset
body: <p>This may be weird, but I would like to somehow combine BETWEEN clause with the table, like in the following example.</p>
  
  <p>Say we have two tables:</p>
  
  <pre><code>Tbl1
  ╔════════╦════════╗
  ║ Value1 ║ Value2 ║
  ╠════════╬════════╣
  ║      1 ║      3 ║
  ║      5 ║      8 ║
  ╚════════╩════════╝
  
  Tbl2
  ╔═══════╦══════╗
  ║ Value ║ Stat ║
  ╠═══════╬══════╣
  ║     1 ║    5 ║
  ║     2 ║    0 ║
  ║     3 ║    8 ║
  ║     5 ║    1 ║
  ║     7 ║    2 ║
  ║    10 ║    0 ║
  ║    11 ║    5 ║
  ║    14 ║    7 ║
  ╚═══════╩══════╝
  </code></pre>
  
  <p>Tbl1 sets the rule for intervals, and Tbl2 sets instances that fall between intervals.
  Here's how it goes:</p>
  
  <ul>
  <li>first interval is 1 to 3, so take all Tbl2 records where Value is between 1 and 3 and sum up Stat</li>
  <li>first interval is 5 to 8, so take all Tbl2 records where Value is between 5 and 8 and sum up Stat</li>
  </ul>
  
  <p>I can do it using multiple selects like this:</p>
  
  <pre><code>SELECT sum(Stat) FROM Tbl2 WHERE [Value] BETWEEN 1 AND 3
  SELECT sum(Stat) FROM Tbl2 WHERE [Value] BETWEEN 5 AND 8
  </code></pre>
  
  <p>Is there a way to sum up everything in just one select?
  I'm using SQL Server 2008 R2, if that makes a difference for you.</p>
  
  <p>Using cursor is out of the question because there's a <strong>LOT</strong> of recors and rules in these two tables.</p>