列1        列2     
  Q1        1    
  Q11       7
  Q2_1      2       
  Q2_2      2       
  Q2_3      2       
  Q31_1     4       
  Q444_3    5      
  Q444_5    5       我想查询结果是: 
  Q1        1 
  Q11       7 
  Q2_1      2      
  Q31_1     4      
  Q444_3    5       

解决方案 »

  1.   

    --> 测试数据: @tb
    declare @tb table (列1 varchar(6),列2 int)
    insert into @tb
    select 'Q1',1 union all
    select 'Q11',7 union all
    select 'Q2_1',2 union all
    select 'Q2_2',2 union all
    select 'Q2_3',2 union all
    select 'Q31_1',4 union all
    select 'Q444_3',5 union all
    select 'Q444_5',5select * from @tb t
    where not exists(select * from @tb where 列2=t.列2 and right(列1,1)< right(t.列1,1))列1     列2
    ------ -----------
    Q1     1
    Q11    7
    Q2_1   2
    Q31_1  4
    Q444_3 5(5 行受影响)
      

  2.   

    select * from tb where charindex('_',列1)=0
    union all 
    select * from tb k 
    where not exists(select * from tb 
    where left(列1,charindex('_',列1)-1)=left(列1,charindex('_',k.列1)-1) 
     and stuff(列1,1,charindex('_',列1),'')<stuff(列1,1,charindex('_',k.列1),'')
      

  3.   

    select min(列1), 列2  from  table group by 列2  
      

  4.   


    select min(列1), 列2  from  table group by 列2