一个表,里面有部分数据重复。现在有三个字段abc,如果a的两行相等,则保留 b值最大的。如果ab均相等,则保留C值最大的。请问怎么实现?
3条回答 默认 最新
创意程序员 2023-05-15 19:12关注用group by,sql参考如下:
select a, case when max_b=min_b then max_c else max_b end max from (select a, max(b) max_b, min(b) min_b from table group by a) t1 left join (select a,b,max(c) max_c from table group by a,b) t2 on t1.a=t2.a and t1.max_b=t2.b子查询t1:根据a分组,找最大b值和最小b值
子查询t2:根据a,b分组,找最大c值
t1 left join t2 on t1.a=t2.a and t1.max_b=t2.b:将具有最大b值和最大c值的记录连起来
case when max_b=min_b then max_c else max_b end:如果最大b值和最小b值相等,取最大c值,否则取最大b值评论 打赏 举报 编辑记录解决 2无用