if object_id('tempdb..#temp') is not null
drop table #temp create table #temp(col1 varchar(100), col2 varchar(100))
insert into #temp
select 'X68.23',''
union all select 'X86.32',''
union all select 'ZA11.30',''
union all select 'ZB11.47',''
go ------ 需求一、按col 4,1,2排序
-- 用 charindex
select col1,col2 from #temp
order by charindex(col2,'4,1,2') -- order by 中用 case
select col1,col2 from #temp
order by
case col2
when '' then 1
when '' then 2
when '' then 3
else 3 + rand()
end -- 用 union
select col1,col2 from #temp where col2 = '' union all
select col1,col2 from #temp where col2 = '' union all
select col1,col2 from #temp where col2 = '' ------ 需求二、 col 4 排第一,其余随便
select col1,col2 from #temp
order by
case col1
when '' then 1
else 1 + rand()
end ------ 需求三、 随机排序
select col1,col2 from #temp
order by newid()
  1. if object_id('tempdb..#temp') is not null
  2. drop table #temp
  3. create table #temp(col1 varchar(100), col2 varchar(100))
  4. insert into #temp
  5. select 'X68.23','4'
  6. union all select 'X86.32','2'
  7. union all select 'ZA11.30','1'
  8. union all select 'ZB11.47','1'
  9. go
  10. ------ 需求一、按col 4,1,2排序
  11. -- 用 charindex
  12. select col1,col2 from #temp
  13. order by charindex(col2,'4,1,2')
  14. -- order by 中用 case
  15. select col1,col2 from #temp
  16. order by
  17. case col2
  18. when '4' then 1
  19. when '1' then 2
  20. when '2' then 3
  21. else 3 + rand()
  22. end
  23. -- 用 union
  24. select col1,col2 from #temp where col2 = '4' union all
  25. select col1,col2 from #temp where col2 = '1' union all
  26. select col1,col2 from #temp where col2 = '2'
  27. ------ 需求二、 col 4 排第一,其余随便
  28. select col1,col2 from #temp
  29. order by
  30. case col1
  31. when '4' then 1
  32. else 1 + rand()
  33. end
  34. ------ 需求三、 随机排序
  35. select col1,col2 from #temp
  36. order by newid()
  37. ORDER BY
    case when state=5 then 2 else 1 end ASC,
    weight_op_time DESC

order by 特殊排序技巧的更多相关文章

  1. mysql如何用order by 自定义排序

    mysql如何用order by 自定义排序 id name roleId aaa bbb ccc ddd eee ,MySQL可以通过field()函数自定义排序,格式:field(value,st ...

  2. Mysql Order By 字符串排序,mysql 字符串order by

    Mysql Order By 字符串排序,mysql 字符串order by ============================== ©Copyright 蕃薯耀 2017年9月30日 http ...

  3. 【Python】 sort、sorted高级排序技巧

    文章转载自:脚本之家 这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Pyth ...

  4. mysql order by 中文 排序

    mysql order by 中文 排序 1. 在MySQL中,我们经常会对一个字段进行排序查询,但进行中文排序和查找的时候,对汉字的排序和查找结果往往都是错误的. 这种情况在MySQL的很多版本中都 ...

  5. 【转载】 python sort、sorted高级排序技巧

    这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Python list内置so ...

  6. mysql select 无order by 默认排序 出现乱序的问题

    原文:mysql select 无order by 默认排序 出现乱序的问题 版权声明:感谢您的阅读,转载请联系博主QQ3410146603. https://blog.csdn.net/newMan ...

  7. Order by 默认排序方式

    --ORDER BY 默认排序方式为升序ASC:SELECT * FROM [TABLE_NAME] ORDER BY [COLUMN_NAME] ESC;--升序DESC:SELECT * FROM ...

  8. Order Siblings by 排序

    在层次查询中,如果想让"亲兄弟"按规矩进行升序排序就需要使用ORDER SIBLINGS BY 这个特定的排序语句,若要降序输出可以在其后添加DESC关键字. 通过这个实验给大家展 ...

  9. 「Python实用秘技07」pandas中鲜为人知的隐藏排序技巧

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第7期 ...

随机推荐

  1. C语言学习13

    快速排序 //快速排序 #include <stdio.h> void quicksort(int a[], int left, int right); void main() { ] = ...

  2. [数据结构]C#基于数组实现泛型顺序表

    前方预警,只完成了顺序表的插入/删除/查找. 错误代码示例: /// <summary> /// 查找顺序表第i个位置的元素 /// 在显示情况中,我们更常用下标 /// </sum ...

  3. js模式(一):单例模式

    function Universe(){ var instance; Universe = function (){ return instance; } Universe.prototype = t ...

  4. Ext修改Confirm弹框按钮的默认值

  5. 大数据学习——yum练习安装jdk

    yum list | grep jdk 安装jdk-1.8.0版本 -openjdk* 安装后,执行java -version 配置环境变量 使用vim /etc/profile 编辑profile文 ...

  6. iOS-runtime-objc_setAssociatedObject(关联对象以及传值)

    例子: static const char kRepresentedObject; - (IBAction)doSomething:(id)sender { UIAlertView *alert = ...

  7. 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing

    acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...

  8. Java设计模式之(设计模式的概述)

    概述: 设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用.设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案.这些解决方案是众多软件开发 ...

  9. 【2018 Multi-University Training Contest 2】

    01: 02: 03: 04:https://www.cnblogs.com/myx12345/p/9394511.html 05: 06: 07:https://www.cnblogs.com/my ...

  10. vim—基本命令1

    ---------------------------------------------------------------2015.07.27 :b 1  -> 切换到当前缓冲区 :2 4 ...