title: Increment a column value in Oracle
body: <p>I need to increment a column value based on certain criteria.</p>
  
  <p>I have 2 tables -</p>
  
  <p>Table A has ID, Name, Date, and so on</p>
  
  <pre><code>Id  Name    Date
  1   ABC 3/1/2014
  2   ABC 4/1/2014
  3   ABC 5/1/2014
  4   DEF 4/1/2014
  5   DEF 5/1/2014
  6   HIJ 1/1/2014
  7   HIJ 2/1/2014
  8   HIJ 3/1/2014
  9   HIJ 4/1/2014
  10  HIJ 5/1/2014
  </code></pre>
  
  <p>Table B has Name(foreign Key), Date(will be same as Table A), counter, and so on.</p>
  
  <pre><code>Name    Date    Counter(the value that I need is in this column, it will be zero by default)
  ABC 3/1/2014    0
  ABC 4/1/2014    1
  ABC 5/1/2014    2
  DEF 4/1/2014    0
  DEF 5/1/2014    1
  HIJ 1/1/2014    0
  HIJ 2/1/2014    1
  HIJ 3/1/2014    2
  HIJ 4/1/2014    3
  HIJ 5/1/2014    4
  </code></pre>
  
  <p>I need to increment the counter as above shown in the table. So for each name the counter should reset and start from 0 and the dates for each names will be different, so I need to take that into account(meaning I cannot assume 1/31/2014 to be 0 for all the names. The counter would end at say 5/1/2014. So I need to update the counter till the date has reached 5/1/2014.</p>
  
  <p>What is the best way to accomplish this? I am trying to write a procedure, but not sure how I can update the counter for different names in the cursors. I am open to any suggestion. Thanks.</p>
  
  <p><strong>EDIT::: Updated Code</strong></p>
  
  <pre><code>Update Table B, set counter = 0;
  
  declare
  
  cursor rec is
  
  select a.name, a.postingdate, b.counter
  from table A a
  inner join table B b
  on a.name = b.name
  order by a.name, a.date;
  
  begin
  
      open rec;
      LOOP
          fetch rec into v_name, v_date, v_counter;
          exit...........
  
  **&lt;How can I reset my counter here for different names&gt;**
          if date &lt; to_date ('01-may-2014', 'dd-mon-yy') then v_counter := v_counter +1;  
          end if;
  
          update table b set counter = v_counter where current of rec;
      END LOOP;
      commit;
  END;
  </code></pre>