一个筛选的SQL语句

muyebo 2010-08-16 05:44:20
table A
NUM ID
1 BBB
2 AAA
3 BBB
4 BBB

TABLE b
NUM ISLATE time
1 TRUE 08-02
2 FALSE 08-23
3 TRUE 09-14
4 FALSE 06-23

问 如何找出最后一次(time)为true的所有ID数据呢
关联用num
即最后一次都没迟到的人。
...全文
98 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamltd 2010-08-16
  • 打赏
  • 举报
回复
从题意来看2楼就搞定了啊

[Quote=引用 2 楼 csuxp2008 的回复:]
每个人最后一次?

SQL code

select id
from a
where num
in
( select num
from
(
select b.*,row_number() over(partition by num order by time desc) rn……
[/Quote]
cxmcxm 2010-08-16
  • 打赏
  • 举报
回复
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')
dawugui 2010-08-16
  • 打赏
  • 举报
回复
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)
GUOCHENGJUN 2010-08-16
  • 打赏
  • 举报
回复
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
GUOCHENGJUN 2010-08-16
  • 打赏
  • 举报
回复
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
csuxp2008 2010-08-16
  • 打赏
  • 举报
回复
每个人最后一次?

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'
)
pt1314917 2010-08-16
  • 打赏
  • 举报
回复
结果应该是啥?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧