之前http://community.csdn.net/Expert/topic/5254/5254854.xml?temp=.4921381的问题已经由  hellowork(一两清风)   解决,非常感谢,但是如果把原记录改为:  a    b    c
-----------------
 1    1     a
 1    2     b
 1    3     c
 2    4     d
 1    5     e
 2    6     f
 2    7     g
 3    8     h要求结果为:
  a    b    c
-----------------
 1    1     a
      2     b
      3     c
      5     d
 2    4     e
      6     f
      7     g
 3    8     h则无法解决,再问!!!

解决方案 »

  1.   

    declare @t table(a int,b int,c varchar(20))
    insert @t
    select 1,    1,     'a' union all
    select 1,    2,     'b' union all
    select 1,    3,     'c' union all
    select 2,    4,     'd' union all
    select 1,    5,     'e' union all
    select 2,    6,     'f' union all
    select 2,    7,     'g' union all
    select 3,    8,     'h'
    ----查询
    select 
    a = case 
    when b = (select top 1 b from @t where a = x.a order by b) 
    then cast(a as varchar(10)) 
    else '' end,b,c 
    from @t as x
    order by a*1,b---结果
    a          b           c                    
    ---------- ----------- -------------------- 
    1          1           a
               2           b
               3           c
               5           e
    2          4           d
               6           f
               7           g
    3          8           h(所影响的行数为 8 行)
      

  2.   

    Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人),感谢!!!强!!
    呵呵,不过想问一下,a*1有什么作用,另外这段sql是不是相当于一个嵌套,告诉我给你分,当然不告诉也会给,呵呵
      

  3.   

    如果
    order by a,bsql的优化器会认为按照结果的第一个字段排序,这样所有空的都跑到最前面去了
    order by a*1,b
    就是指明先按a字段排序,加了运算(*1)表示必须选择字段
    也可以:
    declare @t table(a int,b int,c varchar(20))
    insert @t
    select 1,    1,     'a' union all
    select 1,    2,     'b' union all
    select 1,    3,     'c' union all
    select 2,    4,     'd' union all
    select 1,    5,     'e' union all
    select 2,    6,     'f' union all
    select 2,    7,     'g' union all
    select 3,    8,     'h'
    ----查询
    select 
    a1 = case 
    when b = (select top 1 b from @t where a = x.a order by b) 
    then cast(a as varchar(10)) 
    else '' end,b,c 
    from @t as x
    order by a,b不过前端也许就不方便了
      

  4.   

    Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人) 太感谢了,不过sorry,还得问你
    “就是指明先按a字段排序,加了运算(*1)表示必须选择字段”,这句不太明白,麻烦在给说说,谢谢,另外,留个msn可以么?等你30分钟,不管怎样,30分钟之后给分,再次感谢!!!!
      

  5.   

    Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人):
    “就是指明先按a字段排序,加了运算(*1)表示必须选择字段”的意思是不是说加了(*1)就不按结果集中a字段排序,而是按时据数据库中记录的a字段值排序???
      

  6.   

    Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人):
    “就是指明先按a字段排序,加了运算(*1)表示必须选择字段”的意思是不是说加了(*1)就不按结果集中a字段排序,而是按数据库中实际记录的a字段值排序???
    刚才有两个字打错了!
      

  7.   

    declare @t table(a int,b int,c varchar(20))
    insert @t
    select 1,    1,     'a' union all
    select 1,    2,     'b' union all
    select 1,    3,     'c' union all
    select 2,    4,     'd' union all
    select 1,    5,     'e' union all
    select 2,    6,     'f' union all
    select 2,    7,     'g' union all
    select 3,    8,     'h'----方法1
    select 
    d = case   /*取个新列名,防止与a同名,以使a能用于排序*/
    when b = (select top 1 b from @t where a = x.a order by b) 
    then cast(a as varchar(10)) 
    else '' end,b,c
    from @t as x
    order by a,b     /*按照a列原值排序,再按b列原值排序*/----方法2:使用排序后的子查询作为查询的数据源(效率要低些)
    select 
    a = case 
    when b = (select top 1 b from @t where a = x.a order by b) 
    then cast(a as varchar(10)) 
    else '' end,b,c 
    from (select top 100 percent * from @t order by a,b) as x    /*子查询表示对源数据进行排序*/
      

  8.   

    这样的sql不是循环也不是嵌套,只是一种相关查询,体现在下面这行代码中:
    when b = (select top 1 b from @t where a = x.a order by b)
    即源表中每扫描一行都判断"b = (select top 1 b from @t where a = x.a order by b)"这个条件是否成立,关联之处在于子查询中的a = x.a
      

  9.   

    再次感谢Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人)、hellowork(一两清风),两位都是高手,分数不太好分啊,hellowork(一两清风)上次已经拿了100了,这次少拿点吧,呵呵
    给个msn啊!!??
      

  10.   

    结果为下面的该怎么写?
     a    b    c
    -----------------
     1    5     d
     2    7     g
     3    8     h