有以下数据
tel     qy
12344   gl
15468   gl
46447   lg
45647   lg
78767   xa
31234   xa
现在想要取出qy相同的号码的一半(不要求是上一半还是下一半),最终的显示结果类似于下表
tel     qy
15468   gl
45647   lg
78767   xa请各位高手帮忙,谢谢!

解决方案 »

  1.   

    select max(tel) as tel,qy from 表 group by qy
      

  2.   

    select max(tel),qy from #t group by qy
      

  3.   

    没看明白需求,建议说明白,如果是取相同数据中的一条用distinct
      

  4.   

    alter table t1 add id int  identity(1,1)
    select * from t1 a where exists(select 1 from t1 where tel=a.tel and a.id>id)
    在删掉 id列 就可以了啊 !!!
      

  5.   

    libin_ftsafe(子陌红尘:TS for Banking Card) 
    是对的 !!
    不好意思刚次看错了 !!
      

  6.   

    不是取一条记录,比如说qy字段中的gl有10个记录的话,需要取出其中的5个
    就是每个qy的数据取出一半,不知道这样说明白了没有?
      

  7.   

    tomyuansir() ( ) 信誉:100  2007-09-14 18:26:23  得分: 0  
     
     
       libin_ftsafe(子陌红尘:TS for Banking Card) 
    是对的 !!
    不好意思刚次看错了 !!
      
    -----------------
    不行的,子陌寫的針對上面數據有效, 如果多一點數據就不行的
      

  8.   

    create table #t
    (tel varchar(10),     qy varchar(10))
    insert into #t
    select '12344' ,  'gl' union all
    select '15468' ,  'gl' union all
    select '45660' ,  'gl' union all
    select '85232' ,  'gl' union all
    select '46447' ,  'lg' union all
    select '45647' ,  'lg' union all
    select '78767' ,  'xa' union all
    select '31234' ,  'xa'select * from #t a 
    where (select count(1) from #t where qy=a.qy and tel>a.tel)<(select count(1)from #t where qy=a.qy)/2
    tel        qy         
    ---------- ---------- 
    45660      gl
    85232      gl
    46447      lg
    78767      xa(4 row(s) affected)
    這樣才可以顯示每一組中數據的一半