如何用sql排除重复结果只取字段最大值的记录?
要求得到的结果(即是pid相同的记录只取id值最大的那一条)。
复制代码 代码如下:
select * from [sheet1$] a
where id not exists (select 1 from [sheet1$] where pid=a.pid and id>a.id)
select a.* from [sheet1$] a inner join (select pid,max(id) as max_id from [sheet1$] group by pid) b on a.pid=b.pid and a.id=b.max_id