title: improve complex query in Teradata
body: <p>I would like to know how to get all the rows from table1 that have a matching row in table3.</p>
  
  <p>Teh structure of the tables is:</p>
  
  <pre><code>table1:
  k1 k2
  
  table2:
  k1 k2 t1 t2 date type
  
  table3:
  t1 t2 date status
  </code></pre>
  
  <p>The conditions are:</p>
  
  <ul>
  <li>k1 and k2 have to match with the corresponding columns in table2.</li>
  <li>In table2 I will only chek those rows where date='today' and type='a'.</li>
  <li>That can return 0, 1 or many rows in table2.</li>
  <li>Looking at t1 and t2 from table 2, I get the rows that match in table3.</li>
  <li>If in table3 date='today' and status='ok', I will return the original row from table1, this is, k1 and k2.</li>
  </ul>
  
  <p>How can I do this query (inner joins, exists, whatever) having into account that the three tables have millions of rows, so it must be as optimal as possible?</p>
  
  <p>I have the query, which is right for sure, but they are too many conditions for Teradata to come with the answer. Too many joins, I think.</p>