order by  
case CEmpType when '2' then 1 when '1' then 2 when '0' then 3 else 4 endorder by 
case when #tt1.ywq is null then 1 else 0 end,#tt1.ywq,
case when #tt2.ywq is null then 1 else 0 end,#tt2.ywq,
case when #tt3.ywq is null then 1 else 0 end,#tt3.ywq,
case when #tt4.ywq is null then 1 else 0 end,#tt4.ywq,
case when #tt5.ywq is null then 1 else 0 end,#tt5.ywq断章了,请取义。
帮忙解释一下,各自是什么意思啊?

解决方案 »

  1.   

    将NULL排在后面,貌似多次一举,SQL默认就将NULL放在后面
      

  2.   

    order by   
    case CEmpType when '2' then 1 when '1' then 2 when '0' then 3 else 4 end 第一个排序是 根据 CEmpType 来排序 他排序是 2,1,3 这样 排的 把CEmpType 为2 的排最前边 紧接着就是 CEmpType 为 1的 然后是 为 0 的其他的排最后order by  
    case when #tt1.ywq is null then 1 else 0 end,#tt1.ywq, 
    case when #tt2.ywq is null then 1 else 0 end,#tt2.ywq, 
    case when #tt3.ywq is null then 1 else 0 end,#tt3.ywq, 
    case when #tt4.ywq is null then 1 else 0 end,#tt4.ywq, 
    case when #tt5.ywq is null then 1 else 0 end,#tt5.ywq 这个是根据 YWQ 列来排序  把该列 为 NULL 的排在不为 NULL 的后边
      

  3.   

    sorry,搞错,默认是排在前面的
      

  4.   

    就是先将NULL值排在后面,然后按ywq升序排
      

  5.   

    就是排序的方法。默认的是按asc进行排序。
    对字符进行大小不同的付值,最后排序。
    就这么点事。。
      

  6.   

    第一个
    排序时以cemptype列值分类
    第二个
    排序时以ywq列值进行分类顺序为0,1,2,...
      

  7.   

    以指定的列进行排序,以 when 值小的置前.
      

  8.   

    我也能理解一点,就是不能特别明白。
    特别是第二种
    order by   
    case when #tt1.ywq is null then 1 else 0 end,#tt1.ywq,  
    如果#tt1.ywq是null,那么得到的是
    order by 1,#tt1.ywq,
    这个是怎么理解的?
    1表示序号吗?
    那么如果都是null,得到的是
    order by 1,#tt1.ywq,1,#tt2.ywq,1,#tt3.ywq,1,#tt4.ywq,1,#tt5.ywq
    又是什么东西?
      

  9.   

    ORDER BY 字段列表还需那么多逻辑判断,估计效率很低啊。
      

  10.   

    我做了个试验 
    在SQL server中,好像 
    Order by 1 表示按第几个字段排序。 
    如果Order by 1,1 会报错。 
    但是,如果用了case,先Order by 一部分(也就是用case 条件约束一下),再1是可以的。 
    所以case when #tt1.ywq is null then 1 else 0 end,#tt1.ywq, 
    表示的是,该字段 如果是null的话,那么按这个数据集里的第一个字段排序(then 1)。否则按0字段排序(else 0)。 
    虽然,直接写Order by 0是错误的。但是在case语句里确是可以使用的。 
    可能应该这么理解:是null的按第一个字段排序,不是null的先不管,后面补充说了按“#tt1.ywq”顺序排序。 
    两种力量的结合,最后得出#tt1.ywq字段先按not null的顺序排序,接着null的内容按第一个字段的顺序排序。 
    我的理解有没有错? 
    SQL达人指教。