要求不是用联合查询
即不是先查n1中为g的,再查为f的,最后查非g非f的,再把3次结果连接
不用这种方法能实现吗有哪位有过类似要求的应用吗
是用什么方法

解决方案 »

  1.   

    order by case n1 
        when 'g' then '0'
        when 'c' then '1'
        else '9' end + n1  
      

  2.   

    select  * from t1 order by case n1 when 'g' then 0 when 'c' then 1 else 2 end
      

  3.   


    declare @T1 table(n1 varchar(5),n2 int)
    insert into @T1
    select 'a',1 union all
    select 'c',1 union all
    select 'b',1 union all
    select 'd',1 union all
    select 'f',1 union all
    select 'g',1 union all
    select 'g',2 union all
    select 'a',1
    select *,'n3'=(case n1 when 'g' then 2 when 'c' then 1 end),'n4'=(case when n1 ='g' or n1 ='c' then 1 else 2 end) from @T1
    order by n4 asc,n3 desc,n1 asc/*
    n1  n2  n3  n4
    --------------------
    g 1 2 1
    g 2 2 1
    c 1 1 1
    a 1 NULL 2
    a 1 NULL 2
    b 1 NULL 2
    d 1 NULL 2
    f 1 NULL 2
    */