SELECT A.* FROM test A,(SELECT aid, MAX(day) max_day FROM test GROUP BY aid) BWHERE A.aid = B.aid AND A.day = B.max_day