sql语句,想根据两列条件保留重复项,该怎么做

img


如图,当hospital_expire_flag出现1时则保留此行,否则取升序列最大值那行

select * from table t where t.hospital_expire_flag=1
union all
select 表字段 from
(
select t.*, rank()over(order by 升序 desc) rn from table t where t.hospital_expire_flag=0
) t1 where t1.rn=1

这个我知道的就是分开做

(select distinct(subject_id)from table  where hospital_expire_flag = 1) union ( select distinct(subject_id)from table where hospital_expire_flag = 0 order by 升序 desc )

降序可以使用吗

你这个 **升序 ** 字段 是查询出来的 还是表结构里面就有这个字段