MySQL – How to Query for ‘First Showed Up As’

group byMySQLselect

Given the following table:

 Date       Trd (INT(10))   Code (VARCHAR(10))  T-Date
 1-Jan-14   1               B                   1-Nov-13
 1-Jan-14   2               B                   3-Oct-13
 1-Jan-14   2               B                   3-Oct-13
 2-Jan-14   1               A                   1-Nov-13
 2-Jan-14   2               A                   3-Oct-13
 2-Jan-14   2               A                   3-Oct-13
 2-Jan-14   3               A                   2-Jan-14
 3-Jan-14   2               A                   3-Oct-13
 3-Jan-14   2               A                   3-Oct-13
 3-Jan-14   3               A                   2-Jan-14
 3-Jan-14   4               C                   3-Jan-14

How do I SELECT, to get the following result:

 Date       Trd (INT(10))   First showed up as Code   In Date       T-Date
 3-Jan-14   2               B                         1-Jan-14      3-Oct-13
 3-Jan-14   3               A                         2-Jan-14      2-Jan-14
 3-Jan-14   4               C                         3-Jan-14      3-Jan-14

So for a given Date (in this case Jan-3rd), GROUP BY Trd and find out with which Code it first showed up, and provide the Date that happened.

I have no idea where to start.

Best Answer

First, find the Trd values for the specific date (using either GROUP BY or DISTINCT):

SELECT DISTINCT Trd
FROM tableX
WHERE Date = '2014-01-03' ;

Then you can use the above as the "driving" subquery to get the first appearance of these Trd values. Basically, this type of queries is often called [greatest-n-per-group] (there's even a tag at SO!) There are many ways to be done, depending on how you want ties to be shown - and resolved. The following shows only one result per Trd and resolves ties on Date by using the ID column:

SELECT 
    '2014-01-03' AS Date,
    t.Trd,
    t.Code,
    t.Date       AS InDate,
    t.T_Date 
FROM
    ( SELECT DISTINCT Trd
      FROM tableX
      WHERE Date = '2014-01-03' 
    ) AS dt
  JOIN tableX AS t
    ON  t.ID =
        ( SELECT ti.ID
          FROM tableX AS ti
          WHERE ti.Trd = dt.Trd
          ORDER BY ti.Date,
                   ti.ID
          LIMIT 1
        ) ;

Two indexes, on (Date, Trd) and on (Trd, Date, ID) would help efficiency.