SQL语句两个表查询数据并取对应ID主键的最近时间的⼀个值select * from (select ename, job, hiredate, e.sal, e.deptno
from emp e,
(select deptno, max(sal) sal from emp group by deptno) t
where e.deptno = t.deptno
select语句查询日期
and e.sal = t.sal)
order by deptno;
select * from (select ename 姓名, job 职业, hiredate ⼊职⽇期, e.sal ⼯资, e.deptno 部门
from emp e,
(select deptno, max(sal) sal from emp group by deptno) t
where e.deptno = t.deptno
and e.sal = t.sal)
order by 部门;
select * from (两表联查 xx ,(select 公司ID,max(时间)from 表) t  where xx.时间=t.时间 and xx.公司ID=t.公司ID)
这个⽅法是把其中⼀个表分组过滤出最新时间和主键customer_id (这就是最新数据generation_date的时间),然后⽤这两个条件(最新
时间和主键)去和另⼀个表联查,联查出来的数据就是最新的了。
select * from(select xx.* from
(select
uu.customer_name,uu.tax_number,pp.start_d_date,ppwork_ation_date,pp.customer_id from
UDP_TS_TRUSTEESHIP pp
left join UDP_TS_CUSTOMER  uu on pp.customer_id = uu.customer_id) xx,
(select max(generation_date)generation_date,customer_id from  UDP_TS_TRUSTEESHIP group by  customer_id ) t
where xx.customer_id=t.customer_id ation_ation_date)order by CUSTOMER_ID
还有⼀种⽅法⽐较简单:
两个表交互后添加条件⽤in
select  pp.customer_id,uu.customer_name,uu.tax_number,pp.start_d_date,ppwork_number
from UDP_TS_TRUSTEESHIP pp left join UDP_TS_CUSTOMER  uu on pp.customer_id = uu.customer_id
where generation_date in (select max(generation_date)generation_date from  UDP_TS_TRUSTEESHIP group by  customer_id) ord

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。