SQL选出每组数据最大值
一数据表中有如下数据:
第一列是编号,第二列是内容,第三列是时间
?F183S-T0201????? 9998??????????????? ?4/26/2006 5:11:50 PM????????????????
?F183S-T0201????? 9998???????????????? 4/29/2006 5:14:04 PM????????????????
?F183S-T0201????? 9998??????????????? ?4/30/2006 5:18:23 PM???????????????
?F183S-T0222????? 9998??????????????? ?4/26/2006 5:11:50 PM????????????????
?F183S-T0222????? 9998?????????????? ??4/29/2006 5:14:04 PM????????????????
?F183S-T0222????? 9998?????????????? ? 4/30/2006 5:18:23 PM????
想选出一组数据:每个编号的最晚一条记录,也就是时间值最大的记录????
?F183S-T0201????? 9998??? 4/30/2006 5:18:23 PM????????
?F183S-T0222????? 9998??? 4/30/2006 5:18:23 PM?
使用inner join:
select A.id,A.content,A.Date from table A?
inner join (select id, max(date) as Date from table group by id) B on A.id=B.id AND A.Date=B.Date