内联结、反联结、左联结再出马解决某流水表中最新记录的问题

有这么一张表:

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日--

原文地址:https://www.cnblogs.com/heyang78/p/12355480.html