title: How to calculate MTD and QTD by YTD value in Oracle
body: <p>There are some data in my table t1 looks like below:    </p>
  
  <pre><code>date    dealer   YTD_Value    
  2018-01   A       1100    
  2018-02   A       2000
  2018-03   A       3000
  2018-04   A       4200
  2018-05   A       5000
  2018-06   A       5500
  2017-01   B        100
  2017-02   B        200
  2017-03   B        500    
  ...      ...       ...
  </code></pre>
  
  <p>then I want to write a SQL to query this table and get below result:    </p>
  
  <pre><code>date    dealer   YTD_Value    MTD_Value       QTD_Value
  2018-01   A       1100           1100           1100
  2018-02   A       2000            900           2000
  2018-03   A       3000           1000           3000
  2018-04   A       4200           1200           1200
  2018-05   A       5000            800           2000
  2018-06   A       5500            500           2500
  2017-01   B        100            100            100
  2017-02   B        200            100            200
  2017-03   B        550            350            550    
  ...       ...      ...            ...            ...
  </code></pre>
  
  <p><code>'YTD'</code> means Year to date<br>
  <code>'MTD'</code> means Month to date<br>
  <code>'QTD'</code> means Quarter to date    </p>
  
  <p>So if I want to calculate <code>MTD</code> and <code>QTD</code> value for dealer <code>'A'</code> in <code>'2018-01'</code>, it should be the same as <code>YTD</code>.    </p>
  
  <p>If I want to calculate <code>MTD</code> value for dealer <code>'A'</code> in <code>'2018-06'</code>, <code>MTD</code> value should equal to <code>YTD</code> value in <code>'2018-06'</code> minus <code>YTD</code> value in <code>'2018-05'</code>. And the QTD value in <code>'2018-06'</code> should equal to <code>YTD</code> value in <code>'2018-06'</code> minus <code>YTD</code> value in <code>'2018-03'</code> or equal to sum <code>MTD</code> value in (2018-04,2018-05,2018-06)</p>
  
  <p>The same rule for other dealers such as B.</p>
  
  <p>How can I write the SQL to achieve this purpose?</p>