title: Select data between date ranges from 2 tables
body: <p>Let suppose I have two table </p>
  
  <ol>
  <li>Taxes</li>
  <li>Products</li>
  </ol>
  
  <p>I want to select sum of product values multiplied by their respective VAT value in taxes table as a percentage where the date of the product is less than date of tax and <code>mDate</code> is less than product date, grouped by VAT or <code>ttid</code></p>
  
  <p>For example </p>
  
  <pre><code>        Taxes                                          products
  
   ttid     date    value  mDate                    Vat    value    date
    0     02-06-11    20    03-07-11                0       15    01-07-11
    1     03-07-11    10    03-08-11                0       15    01-07-11
    0     03-07-11    14    03-08-11                0       15    02-08-11
    1     03-08-11    15    03-09-11                1       10    04-07-11
                                                    1       10    06-08-11
                                                    1       10    08-09-11  --this will not include
  </code></pre>
  
  <p>So my result will look like this.</p>
  
  <pre><code>TTId   Value
  0       3+3+2.1=8.1
  1       1.5+1=2.5
  </code></pre>
  
  <p>The query I have written so far.</p>
  
  <pre><code>select   sum
            ( 
              (cast(t.Value as float)
               *
              (cast(x.value as float)/100)
            )
      as 'Vat'
     ,x.ttid
   from prducts t 
  ,taxes x where x.ttid=t.vat  and  x.mDate&gt;t.date and x.date&lt;=t.date
  group by x.ttid
  </code></pre>
  
  <p>Will somebody help me?</p>