Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have two options.</p> <pre><code>select t1.t1id, max(t1.Name) Name, max(t2.date) Date, (select Value from table2 t22 where t22.date = max(t2.date) and t22.t1id = t2.t1id) Value from table1 t1 left join table2 t2 on t1.t1id = t2.t1id where Name like '%Smith%' group by t2.t1id order by 2 </code></pre> <p>OR</p> <pre><code>select mx.t1id, mx.Name, mx.Date, t2.Value from ( select t1.t1id, max(t1.Name) Name, max(t2.date) Date from table1 t1 left join table2 t2 on t1.t1id = t2.t1id where Name like '%Smith%' group by t2.t1id ) mx left join table2 t2 on (t2.t1id = mx.t1id and t2.date = mx.date) order by 2 </code></pre> <p>Both will produce the same result. The first one takes less code but you might have performance issues with a huge set of data. The second one takes a little more code, but it is also a little more optimized. Notes on the <strong>JOIN</strong> option: </p> <ul> <li>If you go LEFT JOIN (as the example shows), items in Table1 with no correspondent records on Table2 <strong>will be displayed</strong> in the result, but the values in columns Date and Value will be NULL</li> <li>If you go INNER JOIN, items in Table1 with no correspondent records on Table2 <strong>will not be displayed</strong>.</li> </ul> <p><strong>EDIT</strong></p> <p>I missed one of the requirements, which was the Name matching a specific string. The code is now updated. The '%' acts like a wildcard, so it will match names like 'Will Smith' and 'Wail Smithers'. If you want a exact match, remove the wildcards ('%').</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload