需求:资产维修表中同一资产可能维修完继续申请维修,这时候维修状态需要根据最近的维修时间去判断维修状态,所以同一资产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))

结果如下:

最新文章

  1. 【机器学习Machine Learning】资料大全
  2. 玩转 H5 下拉上滑动效果
  3. Android Json处理框架
  4. Mac Pro 修改主机名
  5. POJ1201 Intervals(差分约束系统)
  6. 使用匿名委托,Lambda简化多线程代码
  7. css规范大全
  8. C语言求2的100次方怎么解,大整数运算
  9. python中的subprocess.Popen()使用
  10. ​Installing the Ranger Kafka Plug-in
  11. HDFS简述
  12. error: failed to push some refs to 'https://gitee.com/xxx/xxx'
  13. Webstorm添加新建.vue文件功能并支持高亮vue语法和es6语法
  14. 《分布式任务调度平台XXL-JOB》
  15. 在CentOS 7中搭建Git服务器
  16. go语言字符串处理
  17. linux本地机上传文件到服务器
  18. Unity打安卓包 Android 所有错误解决方案大全(几乎囊括所有打包错误 )
  19. 用JavaScript获取URL参数的方法之一
  20. spring装载配置文件失败报错:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

热门文章

  1. RPD资料库创建(1)
  2. 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree
  3. CMD修复
  4. (转)google Java编程风格中文版
  5. 转:Linux集群-----HA浅谈
  6. VMware 11安装Mac OS X 10.11.5虚拟机以及优化心得
  7. java对象存储管理
  8. std::function,std::bind
  9. Struts2拦截器Interceptor执行顺序理解
  10. 一个令人蛋疼的 Microsoft.AspNet.FriendlyUrls