create table #tb(aid varchar(1),bid varchar(1), date1 datetime, exchage numeric(8,1))
Insert into #tb (aid, bid, date1,exchage)
select 'a','b','2005-01-01  00:00:00','0.1'
union all select 'a','c','2005-01-01  00:00:00','0.2'
union all select 'a','d','2005-01-01  00:00:00','0.3'
union all select 'a','e','2005-01-01  00:00:00','0.5'
union all select 'b','a','2005-03-01  00:00:00','0.5'
union all select 'b','c','2004-03-01  00:00:00','0.6'
union all select 'b','d','2005-03-01  00:00:00','0.8'
union all select 'c','a','2005-04-05  00:00:00','0.1'
union all select 'c','b','2005-04-05  00:00:00','0.5'
union all select 'c','d','2005-04-05  00:00:00','0.2'
union all select 'c','e','2005-04-05  00:00:00','0.2'
union all select 'e','a','2004-01-02  00:00:00','0.1'
union all select 'e','h','2004-02-01  00:00:00','0.5'
union all select 'a','b','2005-08-01  00:00:00','0.1'
union all select 'a','c','2005-08-01  00:00:00','0.2'
union all select 'a','d','2005-08-01  00:00:00','0.3'
union all select 'a','e','2005-08-01  00:00:00','0.5'
union all select 'c','b','2005-07-01  00:00:00','0.1'
union all select 'c','a','2005-07-01  00:00:00','01'
union all select 'c','d','2005-07-01  00:00:00','0.4'
union all select 'c','e','2005-07-01  00:00:00','0.4'
union all select 'd','a','2005-08-01  00:00:00','0.5'
union all select 'd','b','2005-08-01  00:00:00','0.8'
union all select 'd','c','2005-08-01  00:00:00','0.4'
union all select 'd','e','2005-08-01  00:00:00','0.5'
union all select 'd','a','2004-01-01  00:00:00','0.3'
union all select 'd','c','2004-01-01  00:00:00','0.5'
union all select 'd','c','2004-01-01  00:00:00','0.6'
union all select 'd','a','2005-01-01  00:00:00','0.1'
union all select 'd','c','2005-01-01  00:00:00','014'
union all select 'd','b','2005-01-01  00:00:00','0.4'
union all select 'd','e','2005-01-01  00:00:00','0.6'select * from #tbselect a.*
from #tb a
inner join (select aid,bid,date1=max(date1) from #tb group by aid,bid)b
on a.aid=b.aid and a.bid=b.bid and a.date1=b.date1
order by 1,2aid    bid          date1                        exchage
a b 2005-08-01 00:00:00.000 .1
a c 2005-08-01 00:00:00.000 .2
a d 2005-08-01 00:00:00.000 .3
a e 2005-08-01 00:00:00.000 .5
b a 2005-03-01 00:00:00.000 .5
b c 2004-03-01 00:00:00.000 .6
b d 2005-03-01 00:00:00.000 .8
c a 2005-07-01 00:00:00.000 1.0
c b 2005-07-01 00:00:00.000 .1
c d 2005-07-01 00:00:00.000 .4
c e 2005-07-01 00:00:00.000 .4
d a 2005-08-01 00:00:00.000 .5
d b 2005-08-01 00:00:00.000 .8
d c 2005-08-01 00:00:00.000 .4
d e 2005-08-01 00:00:00.000 .5
e a 2004-01-02 00:00:00.000 .1
e h 2004-02-01 00:00:00.000 .5

解决方案 »

  1.   

    我自己已经写出来了:
    select * from test where aid in (select aid from (select aid,date1 ,count(aid) aa from test group by aid,date1 having count(aid)=4) a ) and  date1 in 
    (select bb from (select aid,max(date1) bb from test group by aid) b )
      

  2.   

    select * from test where aid in (select aid from (select aid,date1 ,count(aid) aa from test where date1 in (select bb from (select aid,max(date1) bb from test group by aid) b) group by aid,date1 having count(aid)=4) a ) and  date1 in 
    (select bb from (select aid,max(date1) bb from test group by aid) b )
      

  3.   

    create table #tb(aid varchar(1),bid varchar(1), date1 datetime, exchage numeric(8,1))
    Insert into #tb (aid, bid, date1,exchage)
    select 'a','b','2005-01-01  00:00:00','0.1'
    union all select 'a','c','2005-01-01  00:00:00','0.2'
    union all select 'a','d','2005-01-01  00:00:00','0.3'
    union all select 'a','e','2005-01-01  00:00:00','0.5'
    union all select 'b','a','2005-03-01  00:00:00','0.5'
    union all select 'b','c','2004-03-01  00:00:00','0.6'
    union all select 'b','d','2005-03-01  00:00:00','0.8'
    union all select 'c','a','2005-04-05  00:00:00','0.1'
    union all select 'c','b','2005-04-05  00:00:00','0.5'
    union all select 'c','d','2005-04-05  00:00:00','0.2'
    union all select 'c','e','2005-04-05  00:00:00','0.2'
    union all select 'e','a','2004-01-02  00:00:00','0.1'
    union all select 'e','h','2004-02-01  00:00:00','0.5'
    union all select 'a','b','2005-08-01  00:00:00','0.1'
    union all select 'a','c','2005-08-01  00:00:00','0.2'
    union all select 'a','d','2005-08-01  00:00:00','0.3'
    union all select 'a','e','2005-08-01  00:00:00','0.5'
    union all select 'c','b','2005-07-01  00:00:00','0.1'
    union all select 'c','a','2005-07-01  00:00:00','01'
    union all select 'c','d','2005-07-01  00:00:00','0.4'
    union all select 'c','e','2005-07-01  00:00:00','0.4'
    union all select 'd','a','2005-08-01  00:00:00','0.5'
    union all select 'd','b','2005-08-01  00:00:00','0.8'
    union all select 'd','c','2005-08-01  00:00:00','0.4'
    union all select 'd','e','2005-08-01  00:00:00','0.5'
    union all select 'd','a','2004-01-01  00:00:00','0.3'
    union all select 'd','c','2004-01-01  00:00:00','0.5'
    union all select 'd','c','2004-01-01  00:00:00','0.6'
    union all select 'd','a','2005-01-01  00:00:00','0.1'
    union all select 'd','c','2005-01-01  00:00:00','014'
    union all select 'd','b','2005-01-01  00:00:00','0.4'
    union all select 'd','e','2005-01-01  00:00:00','0.6'select * from #tb where 
    aid in (select aid from 
    (select aid,date1 ,count(aid) aa from #tb 
    where date1 in (select bb from (select aid,max(date1) bb from #tb group by aid) b) group by aid,date1 having count(aid)=4) a ) 
    and  date1 in (select bb from (select aid,max(date1) bb from #tb group by aid) b )