内联结、反联结、左联结再出马解决某流水表中最新记录的问题
有这么一张表:
create table tb( id number(4,0) not null primary key, padid nvarchar2(20) not null, inputdate date not null, dosid integer not null )
可以这样充值:
insert into tb(id,padid,inputdate,dosid) values('1','001',to_date('2020-01-23','yyyy-MM-dd'),'398'); insert into tb(id,padid,inputdate,dosid) values('2','001',to_date('2020-01-24','yyyy-MM-dd'),'442'); insert into tb(id,padid,inputdate,dosid) values('3','001',to_date('2020-01-25','yyyy-MM-dd'),'90'); insert into tb(id,padid,inputdate,dosid) values('4','001',to_date('2020-01-26','yyyy-MM-dd'),'178'); insert into tb(id,padid,inputdate,dosid) values('5','002',to_date('2020-01-23','yyyy-MM-dd'),'444'); insert into tb(id,padid,inputdate,dosid) values('6','002',to_date('2020-01-24','yyyy-MM-dd'),'15'); insert into tb(id,padid,inputdate,dosid) values('7','002',to_date('2020-01-25','yyyy-MM-dd'),'145'); insert into tb(id,padid,inputdate,dosid) values('8','002',to_date('2020-01-26','yyyy-MM-dd'),'196'); insert into tb(id,padid,inputdate,dosid) values('9','003',to_date('2020-01-24','yyyy-MM-dd'),'22'); insert into tb(id,padid,inputdate,dosid) values('10','003',to_date('2020-01-25','yyyy-MM-dd'),'22'); insert into tb(id,padid,inputdate,dosid) values('11','004',to_date('2020-01-24','yyyy-MM-dd'),'33'); insert into tb(id,padid,inputdate,dosid) values('12','004',to_date('2020-01-25','yyyy-MM-dd'),'33'); insert into tb(id,padid,inputdate,dosid) values('13','004',to_date('2020-01-27','yyyy-MM-dd'),'33');
若要求padid相同时,inputdate最新的一条记录,可以有下面三种解决方案:
内联结方案:
select a.* from tb a, (select padid,max(inputdate) as inputdate from tb group by padid) b where a.padid=b.padid and a.inputdate=b.inputdate
SQL> select a.* 2 from tb a, 3 (select padid,max(inputdate) as inputdate 4 from tb 5 group by padid) b 6 where a.padid=b.padid and a.inputdate=b.inputdate; ID PADID INPUTDATE DOSID ---------- ---------------------------------------- -------------- ---------- 4 001 26-1月 -20 178 8 002 26-1月 -20 196 13 004 27-1月 -20 33 10 003 25-1月 -20 22
反联结方案:
select a.* from tb a where not exists (select null from tb b where a.padid=b.padid and b.inputdate>a.inputdate)
SQL> select a.* 2 from tb a 3 where not exists (select null from tb b where a.padid=b.padid and b.inputdate>a.inputdate); ID PADID INPUTDATE DOSID ---------- ---------------------------------------- -------------- ---------- 13 004 27-1月 -20 33 10 003 25-1月 -20 22 4 001 26-1月 -20 178 8 002 26-1月 -20 196
左联结方案::
select a.* from tb a left join tb b on a.padid=b.padid and b.inputdate>a.inputdate where b.inputdate is null
SQL> select a.* 2 from tb a 3 left join tb b 4 on a.padid=b.padid and b.inputdate>a.inputdate 5 where b.inputdate is null; ID PADID INPUTDATE DOSID ---------- ---------------------------------------- -------------- ---------- 13 004 27-1月 -20 33 10 003 25-1月 -20 22 4 001 26-1月 -20 178 8 002 26-1月 -20 196
这三个技巧在流水表的使用中容易见到。
相关网页:
https://bbs.csdn.net/topics/395825769?page=1#post-410739481
--2020年2月24日--
分类:
Oracle.简易Sql文
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2016-02-24 转载:XPath基本语法
2016-02-24 爪哇国新游记之三十四----Dom4j的XPath操作