title: Filtering inside a FOREACH block by a condition value calculated inside the same block in PIG
body: <p>I have a log dataset, and I need to filter out all the log entries for an equipment after a failure (Action = 2).</p>
  
  <p>In this example:</p>
  
  <pre><code>EquipId, ScvId, Action, TimeStamp
  Ag,01,1,14-01-01 0:00:01
  Ag,01,1,14-01-02 0:00:01
  Ag,01,2,14-01-03 0:00:01
  Ag,01,1,14-01-04 0:00:01
  Ag,01,1,14-01-05 0:00:01
  Ag,01,2,14-01-06 0:00:01
  Ag,01,1,14-01-07 0:00:01
  Ra,01,1,14-01-01 0:00:01
  Ra,01,1,14-01-02 0:00:01
  Ra,01,1,14-01-03 0:00:01
  Ra,01,2,14-01-04 0:00:01
  Fe,01,2,14-01-03 0:00:01
  Fe,01,1,14-01-03 0:00:02
  Fe,01,1,14-01-04 0:00:01
  Lu,01,1,14-01-05 0:00:01
  Lu,01,1,14-01-04 0:00:01
  Lu,01,1,14-01-05 0:00:01
  </code></pre>
  
  <p>The expected output would be</p>
  
  <pre><code>Ag,01,1,14-01-01 0:00:01
  Ag,01,1,14-01-02 0:00:01
  Ag,01,2,14-01-03 0:00:01
  Ra,01,1,14-01-01 0:00:01
  Ra,01,1,14-01-02 0:00:01
  Ra,01,1,14-01-03 0:00:01
  Ra,01,2,14-01-04 0:00:01
  Fe,01,2,14-01-03 0:00:01
  Lu,01,1,14-01-05 0:00:01
  Lu,01,1,14-01-04 0:00:01
  Lu,01,1,14-01-05 0:00:01
  </code></pre>
  
  <p>I was trying to program it in a single FOREACH block like this:</p>
  
  <pre><code>rawData = LOAD './test.csv'  USING PigStorage(',') AS (equipId:chararray, svcId:chararray, action:chararray, date:chararray);
  
  equipDataGrp = GROUP rawData BY equipId;
  
  minFail = FOREACH equipDataGrp {
  
      actionFail = FILTER rawData BY action == '2';
      minFailDate = MIN(actionFail.date);
      prevActionsFail = FILTER rawData BY date &lt;= minFailDate;
  
  
      GENERATE group as equipId, FLATTEN(prevActionsFail.date);
  
  };
  </code></pre>
  
  <p>and I get the following error:</p>
  
  <pre><code>2014-03-05 11:08:11,720 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1000: 
  &lt;line 36, column 28&gt; Invalid field reference. Referenced field [date] does not exist in schema: .
  </code></pre>
  
  <p>If I hardcode the date as:</p>
  
  <pre><code>minFail = FOREACH equipDataGrp {
  
      actionFail = FILTER rawData BY action == '2';
      minFailDate = MIN(actionFail.date);
      prevActionsFail = FILTER rawData BY date == '14-01-03 0:00:01';
  
  
      GENERATE group as equipId, FLATTEN(prevActionsFail.date);
  
  };
  </code></pre>
  
  <p>I get response:</p>
  
  <pre><code>(Ag,14-01-03 0:00:01)
  (Fe,14-01-03 0:00:01)
  (Ra,14-01-03 0:00:01)
  </code></pre>
  
  <p>Any suggestion?</p>
  
  <p>Thanks in advance!</p>