34,590
社区成员
发帖
与我相关
我的任务
分享
select a.code from (
select a.code,max(b.time) time from
from 表1 a join 表2 b on a.num = b.num) t
order by time
select a.code from (
select a.code,min(b.time) time from
from 表1 a join 表2 b on a.num = b.num) t
order by time
select code
from tabA
order by (select time from tabB x where num=tabA.num
and not exists(select 1 from tabB where num=x.num and time >x.time )
)
select distinct a.code
from 表1 a left join 表2 b
on a.num=b.num
where not exists(select 1 from 表2 where num=a.num and time >b.time )
select distinct a.code,b.time
from 表1 a left join 表2 b
on a.num=b.num
where not exists(select 1 from 表2 where num=a.num and time >b.time )
order by b.time
select code
from tabA
order by (select top 1 time from tabB where num=tabA.num order by time)
select distinct a.code
from 表1 a left join 表2 b
on a.num=b.num
where not exists(select 1 from 表2 where num=a.num and time >b.time )
order by b.time
select distinct code from ta a,tb b where a.num=b.num order by time
if object_id('[tb1]') is not null drop table [tb1]
create table [tb1]([code] varchar(10),[num] varchar(10))
insert [tb1]
select '101','A01' union all
select '102','A01' union all
select '101','A02' union all
select '103','A02'
go
if object_id('[tb2]') is not null drop table [tb2]
create table [tb2]([time] datetime,[num] varchar(10))
insert [tb2]([time],[num])
values('2010-07-05 13:51:29.920','A01')
insert [tb2]([time],[num])
select '2010-07-01 17:28:16.920','A02'
go
select tb1.num,tb2.time from tb2,tb1 where tb2.num=tb1.num
group by tb1.num,tb2.time order by tb2.time desc
go
num time
---------- -----------------------
A01 2010-07-05 13:51:29.920
A02 2010-07-01 17:28:16.920
if object_id('tb') is not null drop table tb
create table tb(code int,num char(4))
insert tb
select 101,'A01' UNION ALL
select 102,'A01' UNION ALL
select 101,'A02' union all
select 103,'A02'
if object_id('tc') is not null drop table tc
create table tc([time] datetime,num char(4))
insert tc
select '2010-07-05 13:51:29','A01' UNION ALL
SELECT '2010-07-01 17:28:16','A02'
select code from (select a.code,max(b.[time]) 'time' from tb a join tc b on a.num=b.num group by a.code) t order by t.[time]
--是max,还是min你自己选吧
--这是max的结果
code
-----------
103
101
102
(3 row(s) affected)