select 
*
from 
T a
order by (select count(1) from T where col2=a.col2 and col1<=a.col1) 

解决方案 »

  1.   

    declare @T table(col1 nvarchar(10), col2 int)
    insert @T select 'A',   1 
    insert @T select 'B',   1 
    insert @T select 'C',   3 
    insert @T select 'D',   2 
    insert @T select 'E',   3 
    insert @T select 'F',   2 
    insert @T select 'G',   4 select  

    from  
    @T a 
    order by (select count(1) from @T where col2=a.col2 and col1 <=a.col1),col2
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)col1       col2        
    ---------- ----------- 
    A          1
    D          2
    C          3
    G          4
    B          1
    F          2
    E          3(所影响的行数为 7 行)
      

  2.   

    declare @T table(col1 nvarchar(10), col2 int)
    insert @T select 'A',   1 
    insert @T select 'B',   1 
    insert @T select 'C',   3 
    insert @T select 'D',   2 
    insert @T select 'E',   3 
    insert @T select 'F',   2 
    insert @T select 'G',   4 select  

    from  
    @T a 
    order by (select count(1) from @T where col2=a.col2 and col1 >=a.col1)desc,col2 asc(所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)
    (所影响的行数为 1 行)col1       col2        
    ---------- ----------- 
    A          1
    D          2
    C          3
    B          1
    F          2
    E          3
    G          4(所影响的行数为 7 行)
      

  3.   

    create table tb(列1 varchar(10) , 列2 int)
    insert into tb values('A',   1 )
    insert into tb values('B',   1 )
    insert into tb values('C',   3 )
    insert into tb values('D',   2 )
    insert into tb values('E',   3 )
    insert into tb values('F',   2 )
    insert into tb values('G',   4 )
    goselect * from tb t order by (select count(*) from tb where 列2 = t.列2 and 列1 < t.列1) + 1 , 列2drop table tb/*
    列1         列2          
    ---------- ----------- 
    A          1
    D          2
    C          3
    G          4
    B          1
    F          2
    E          3(所影响的行数为 7 行)
    */
      

  4.   

    create table tb(列1 varchar(10) , 列2 int)
    insert into tb values('A',   1 )
    insert into tb values('B',   1 )
    insert into tb values('C',   3 )
    insert into tb values('D',   2 )
    insert into tb values('E',   3 )
    insert into tb values('F',   2 )
    insert into tb values('G',   4 )
    goselect * from tb t order by (select count(*) from tb where 列2 = t.列2 and 列1 >= t.列1) + 1 desc, 列2drop table tb/*
    列1         列2          
    ---------- ----------- 
    A          1
    D          2
    C          3
    B          1
    F          2
    E          3
    G          4(所影响的行数为 7 行)
    */
      

  5.   


    declare @T table(col1 nvarchar(10), col2 int)
    insert @T select 'A',   1 
    insert @T select 'B',   1 
    insert @T select 'C',   3 
    insert @T select 'D',   2 
    insert @T select 'E',   3 
    insert @T select 'F',   2 
    insert @T select 'G',   4 select * from 
    (select px=(select count(1) from @t where col2=a.col2 and col1>=a.col1),* from @t a)b
    order by px desc,col2
      

  6.   

    -----
    如果第一列没有大小关系时,只用生成临时表增加一个自增列
    2005可用row_number()over(partition by col2 order by col2)--生成序号