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断章了,请取义。
帮忙解释一下,各自是什么意思啊?
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断章了,请取义。
帮忙解释一下,各自是什么意思啊?
解决方案 »
- sql server如何写带参数的游标?
- 求助用sql查询语句查询两关联表的问题
- 这样的SQL语句要怎么写
- sql server中int,nvarchar的初始默认值是多少?
- 想要建立的数据库和另一台电脑上的数据库一样,应该怎么做呢?
- 为什么要做表内关联
- sql机出问题
- 在用C++编写SQL SERVER的扩展存储过程时遇到问题
- 高分求教:请问怎样执行存储过程在不同数据库间存取数据
- ORA-01401: 插入的值对于列过大
- 怎样恢复UPDATE后的数据
- 程序越编越大,修改时只是面对一大堆vb代码翻来翻去很不方便,到底写软件走怎样的路子才对,修改才容易?是从需求分析-概要设计-详细设计-...这样走下去呢,还是随便一点好呢。哪有一个开发模式可供参考参考,从分析到设计再到实现、测试等。怎样才能理清开发思路,修
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 的后边
对字符进行大小不同的付值,最后排序。
就这么点事。。
排序时以cemptype列值分类
第二个
排序时以ywq列值进行分类顺序为0,1,2,...
特别是第二种
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
又是什么东西?
在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达人指教。