接下(SQL中遇到多条相同内容只取一条的最简单实现)多字段情况

接上(SQL中遇到多条相同内容只取一条的最简单实现)多字段情况

上面的例子中是只有一个字段不相同,假如有两个字段出现相同呢?要求查处第三个字段的最大值该如何做呢?

其实很简单,在原先的基础上稍微做下修改即可:

原先的SQL语句:

select * from timeand as a where not exists(select 1 from timeand where a.time = time and a.total<total);

可修改为:

select * from timeand as a where not exists(select 1 from timeand where a.time = time and (a.total<total or (a.total=total and a.outtotal<outtotal)));

其中outtotal是另外一个字段,为Int类型