ORACLE清除某一字段重复的数据(选取重复数据中另一个字段时期最大值)
需求:资产维修表中同一资产可能维修完继续申请维修,这时候维修状态需要根据最近的维修时间去判断维修状态,所以同一资产ID下会出现重复的数据(维修审批通过,维修审批未通过),或者可能不出现(未申请维修),所以需要查询资产维修表中未重复的数据和重复的数据中申请维修日期最近的数据,方法如下:
资产表如下:
1、资产维修中所有的数据
select * from ASSET_MAINTAIN t
结果如下:
2、资产维修中可能相同的数据
select a.* from ASSET_MAINTAIN a inner join ASSET_MAINTAIN b on a.asset_id=b.asset_id and a.rowid!=b.rowid
结果如下:
3、查询相等的数据中保管日期最大的一条
select * from ASSET_MAINTAIN where maintain_start_date in (select max(n.maintain_start_date) from ASSET_MAINTAIN n group by n.asset_id having count(n.asset_id) > 1)
结果如下:
4、查询资产维修中所有的数据(不包括相同的数据)
select * from ASSET_MAINTAIN t where t.asset_id not in(select a.asset_id from ASSET_MAINTAIN a inner join ASSET_MAINTAIN b on a.asset_id=b.asset_id and a.rowid!=b.rowid)
最后,将相同字段,不同数据的两个子查询相关联:
(select * from ASSET_MAINTAIN t where t.asset_id not in(select a.asset_id from ASSET_MAINTAIN a inner join ASSET_MAINTAIN b on a.asset_id=b.asset_id and a.rowid!=b.rowid)) union all
(select * from ASSET_MAINTAIN where maintain_start_date in (select max(n.maintain_start_date) from ASSET_MAINTAIN n group by n.asset_id having count(n.asset_id) > 1))
结果如下:
最新文章
- 【机器学习Machine Learning】资料大全
- 玩转 H5 下拉上滑动效果
- Android Json处理框架
- Mac Pro 修改主机名
- POJ1201 Intervals(差分约束系统)
- 使用匿名委托,Lambda简化多线程代码
- css规范大全
- C语言求2的100次方怎么解,大整数运算
- python中的subprocess.Popen()使用
- ​Installing the Ranger Kafka Plug-in
- HDFS简述
- error: failed to push some refs to 'https://gitee.com/xxx/xxx'
- Webstorm添加新建.vue文件功能并支持高亮vue语法和es6语法
- 《分布式任务调度平台XXL-JOB》
- 在CentOS 7中搭建Git服务器
- go语言字符串处理
- linux本地机上传文件到服务器
- Unity打安卓包 Android 所有错误解决方案大全(几乎囊括所有打包错误 )
- 用JavaScript获取URL参数的方法之一
- spring装载配置文件失败报错:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException
热门文章
- RPD资料库创建(1)
- 【BZOJ-2843&;1180】极地旅行社&;OTOCI Link-Cut-Tree
- CMD修复
- (转)google Java编程风格中文版
- 转:Linux集群-----HA浅谈
- VMware 11安装Mac OS X 10.11.5虚拟机以及优化心得
- java对象存储管理
- std::function,std::bind
- Struts2拦截器Interceptor执行顺序理解
- 一个令人蛋疼的 Microsoft.AspNet.FriendlyUrls