现在有一个视图如图,现在我想要vessel_cn这一列不允许重复,如果重复则取seain列大的那一条记录,如果seain列的值相同则取seaout列大的那一条记录,如果seaout列值相同则取leng列大的那一条记录,不可能出现两列完全相同的情况。 比如图中第135 136行记录的vessel_cn值相同,所以应该去掉seain值小的135行。 求写sql语句对这个视图进行上述去重复选择。视图名叫v_plan
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
方式一:试试用max聚合: 顺序:max聚合leng-->max聚合seaout-->max聚合seain sql如下: select C.vessel_cn,C.seain,C.maxSeaout,C.maxLength,max(C.seain) as maxSeain from ( select B.vessel_cn,B.seain,B.maxLength,max(B.seaout) as maxSeaout from ( select A.vessel_cn,A.seain,A.seaout,max(A.leng) as maxLength from 你的视图 A group by A.vessel_cn,A.seain,A.seaout ) B group by B.vessel_cn,B.seain,B.maxLength ) C group by C.vessel_cn,C.maxSeaout,C.maxLength
(2)方案二:根据你列出的条件用子查询过滤掉重复的记录,sql: select A.vessel_cn,A.seain,A.seaout,A.leng from 你的视图 A where not exists (select 1 from 你的视图 B where B.vessel_cn=A.vessel_cn and B.seain=A.seain and B.seaout=A.seaout and B.leng>A.leng) and not exists (select 1 from 你的视图 C where C.vessel_cn=A.vessel_cn and C.seain=A.seain and C.seaout>A.seaout and C.leng=A.leng) and not exists (select 1 from 你的视图 D where D.vessel_cn=A.vessel_cn and D.seain>A.seain and D.seaout=A.seaout and D.leng=A.leng)