select identity(int,1,1) as id , b1 into #tmp from 
(select top 3 b1 from table2 order by time desc)
a
order by b1select * from #tmpdrop table #tmp

解决方案 »

  1.   

    根table1没关系吧,好像table2的数据也有误,应该是
    id b1 time
    1  data1 2004.3.13
    1  data2 2004.3.12
    1  data3 2004.3.11
    2  data4 2004.3.13
    3  data5 2004.3.14select top 5 a.id,a.b1 from table1 a,(select id,max(time)as time from table2 group by id) b where a.id=b.id and a.time=b.time order by a.id
      

  2.   

    能不能不用临时表
    两条sql语句也行啊。
      

  3.   

    select a.id,a.b1 
    from 表2 a join 
         ( select id,max([time]) as maxtime
           from 表2
           group by id
         ) b 
         on a.id=b.id and a.time=b.maxtime  
      

  4.   

    楼上再加order by a.id就可以了,thank you