title: SQL SUM between date range in one table
body: <p>I want to <code>sum</code> cost based on <code>ProductId</code> and <code>Date</code>. </p>
  
  <p>My date format is :<code>YY/MM/DD</code> and it is <code>date</code> The data looks like this:</p>
  
  <pre><code>DateFrom     DateTo         Cost     ProductId
  07/12/12    | 07/12/31 |    120,5   | 1 
  01/12/12    | 11/12/31 |    220 5   | 1 
  01/12/12    | 11/12/31 |    120,5   | 1 
  08/12/12    | 08/12/31 |    140,5   | 2 
  11/12/12    | 11/12/31 |    140,5   | 2 
  11/12/12    | 11/12/31 |    140,5   | 2 
  </code></pre>
  
  <p>The output i want to get should look like this:</p>
  
  <pre><code>FirstDateFrom   LastDateTo   SumOfCostFromSameYear   ProductId
  07/12/12       | 07/12/31   |   120,5              | 1  
  01/12/12       | 11/12/31   |   341                | 1  
  08/12/12       | 08/12/31   |   140,5              | 2  
  11/12/12       | 11/12/31   |   281                | 2  
  </code></pre>
  
  <p>As you can see, i want to have sum of cots from every <code>year</code> in my output. </p>
  
  <p>I started to write some <code>sql</code>:</p>
  
  <pre><code>SELECT DateFrom, DateTo, ProductId, SUM(cost) FROM CostOfSomething WHERE ...
  </code></pre>
  
  <p>But i dont know how <code>WHERE</code> statement should look like for my scenerio</p>
  
  <p>EDIT:
  Create script of this table:</p>
  
  <pre><code>create table CostOfSomething(
  Id NUMBER PRIMARY KEY,
  DateTo DATE,
  DateFrom DATE,
  Cost NUMBER(6,2),
  ProductId NUMBER REFERENCES product(ProductId)
  );
  </code></pre>