title: Apache PIG - Count(*) Group by ID and Creation of a Ranking
body: <p>​Hi experts,</p>
  
  <p>I've this dataset:</p>
  
  <pre><code>Field_A Field_B    DATE
  John     1       01-01-2016
  John     1       05-01-2016
  Cate     1       05-01-2016
  Cate     4       01-01-2016
  Cate     6       05-01-2016
  Perdi    4       01-01-2016
  </code></pre>
  
  <p>And I'm trying to calculate the Count(*) of each Field_A and create a rank based on Field A and Date. Basically I want to return this:</p>
  
  <pre><code>Field_A Count   Rank    Field_B
  John      2      1        1
  John      2      2        1
  Cate      3      3        1
  Cate      3      4        4
  Cate      3      3        6
  Perdi     1      5        4
  </code></pre>
  
  <p>For that I'm trying with this code:</p>
  
  <pre><code>DATA  = load '...'
  AS
            (Field_A:Int, 
            FIELD_B:Int,
            DATE:CHARARRAY);
  A = rank DATA BY Field_A;
  B = GROUP A BY $0;
  C = foreach B {
       CNT = COUNT(A.Field_A);
       generate $0, CNT;
  }
  D = join A by $0, C by $0;
  E = rank D BY DATE,Field_A DENSE;
  F = foreach E generate $0 AS RANK,Field_A,CNT;
  DUMP F;
  </code></pre>
  
  <p>But I'm getting the following error:</p>
  
  <pre><code>&lt;file script.pig, line 35, column 69&gt; Invalid field projection. Projected field [CNT] does not exist in schema;
  </code></pre>
  
  <p>How can I solve this?</p>
  
  <p>Many thanks!</p>