题目如下:
create table  t1 ( a char(10),b char(10) 
)insert into t1
select '2006-4','100'
union all
select  '2006-4','101'
union all select '2006-4','102'
union all select '2006-4','103'
union all select  '2006-5','105'
union all select'2006-5','106'
要求出来的结果是:要求以下:a中的头两条记录2006-4 100
2006-4 101
2006-5 105
2006-5 106给出的答案是
select a.* from t1 a where a.b in(select top 2 b from t1 where a=a.a order by b)我想不明白的是
select top 2 b from t1 where a=t1.a order by b的结果才是
100       
101       
为什么总的SQL里面能出来
下面数据
2006-5 105
2006-5 106

解决方案 »

  1.   

    select a.* from t1 a where a.b in(select top 2 b from t1 where a=a.a order by b)注意:子查询中有条件,即子查询表的a与外部查询的a相同,即对于不同的a,会选择出不同的2个b
      

  2.   

    按A分类取出头两条.如果是只取头两条.
    select top 2 * from t1
    select top 2 * from t1 order by b (desc)