title: SQL Sum Sales for a week based on previous week range
body: <p>Need help with SQL query in resolving the below problem:</p>
  
  <p>Input table has Product ID, Week and sales which is unique, while columns Start and End Week column are the range based on which I want to sum up sales for that particular week.</p>
  
  <p><img src="https://i.stack.imgur.com/SUYMG.jpg" alt="input table"></p>
  
  <p>From the Input table we want to extract the Product ID along with Week and get the sum of sales based on the week being between start week and end week range.</p>
  
  <p>The Sales value against each Product ID and Week is the sum of sales based on the corresponding start and end week for that product and week combination in the input table.</p>
  
  <p><img src="https://i.stack.imgur.com/7yTL5.jpg" alt="output table"></p>
  
  <p>I was trying to do a self join on the input table but realized it would not work as I need to join on both Product ID and Week which will nullify the objective.</p>
  
  <pre><code>Select a.Product ID, a.Week, Sum(a.Sales) 
  from Input as a, Input as b 
    where a.Product ID = b.Product ID 
    and a.Week between b.Start Week and b.End Week 
  group by 1,2
  </code></pre>