record_date
has no duplicates within a group:think of it as of filtering. Simpliy get (WHERE) one (MIN(record_date)
) row from the current group:
SELECT * FROM t t1 WHERE record_date = (
select MIN(record_date)
from t t2 where t2.group_id = t1.group_id)
record_date
within a group:filter out non-min rows (see above)
then (AND) pick only one from the 2+ min record_date
rows, within the given group_id
. E.g. pick the one with the min unique key:
AND key_id = (select MIN(key_id)
from t t3 where t3.record_date = t1.record_date
and t3.group_id = t1.group_id)
so
key_id | group_id | record_date | other_cols
1 | 18 | 2011-04-03 | x
4 | 19 | 2009-06-01 | a
8 | 19 | 2009-06-01 | e
will select key_id
s: #1
and #4