27,579
社区成员
发帖
与我相关
我的任务
分享
select * from
a where id in
(select b.id from b
(select id,max(time) time from b group by id) c where b.id=c.id and b.time=c.time and b.islate='true')
select m.* , n.* from a m , b n
where m.num = n.num and n.ISLATE = 'true' and n.time = (select max(time) from b where num = n.num and ISLATE = 'true')
select m.* , n.* from a m , b n
where m.num = n.num and n.ISLATE = 'true' and not exists (select 1 from b where num = n.num and ISLATE = 'true' and time > n.time)
select m.* , n.* from a m left join b n
on m.num = n.num and n.ISLATE = 'true' and n.time = (select max(time) from b where num = n.num and ISLATE = 'true')
select m.* , n.* from a m left join b n
on m.num = n.num and n.ISLATE = 'true' and not exists (select 1 from b where num = n.num and ISLATE = 'true' and time > n.time)
IF NOT OBJECT_ID('A') IS NULL
DROP TABLE A
GO
IF NOT OBJECT_ID('B') IS NULL
DROP TABLE B
GO
create table A (num int,id char(30))
insert into A
select 1,'BBB' union all
select 2,'AAA'union all
select 3,'BBB' union all
select 4,'BBB'
go
create table B
(NUM int ,ISLATE CHAR(10) ,TIME datetime)
go
insert into B
select 1,'TRUE','2010-7-31'union all
select 2,'FALSE','2010-08-23' UNION ALL
SELECT 3,'TRUE','2010-09-04'UNION ALL
SELECT 4,'TRUE','2010-06-23'
select c.id from a c inner join (select a.* from b a where exists(select * from b where time>=a.time) and a.ISLATE='false ') d
on c.num=d.num
GO
--id
--AAA
select id
from a
where num
in
( select num
from
(
select b.*,row_number() over(partition by num order by time desc) rn
from b
) c
where rn=1 and islate ='true'
)