现在有一个视图如图,现在我想要vessel_cn这一列不允许重复,如果重复则取seain列大的那一条记录,如果seain列的值相同则取seaout列大的那一条记录,如果seaout列值相同则取leng列大的那一条记录,不可能出现两列完全相同的情况。
比如图中第135 136行记录的vessel_cn值相同,所以应该去掉seain值小的135行。
求写sql语句对这个视图进行上述去重复选择。视图名叫v_plan
关于去重复记录的sql
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- tearhero 2013-09-22 12:54关注
方式一:试试用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)本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#python#的问题:求帮写python代码
- ¥20 MATLAB画图图形出现上下震荡的线条
- ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
- ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
- ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
- ¥15 perl MISA分析p3_in脚本出错
- ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
- ¥15 ubuntu虚拟机打包apk错误
- ¥199 rust编程架构设计的方案 有偿
- ¥15 回答4f系统的像差计算