求一个关于排序的sql语句

zhongzuo1981 2011-02-12 11:29:26
表结构
id bookCode
106 NULL
107 201001001
108 NULL
109 201001002
110 201001003
111 201002001
id是自增主键,bookCode到某一流程才会有值(9位的数字)
排序要求第一排序bookCode升序,第二排序id降序。

现在的结果是
108 NULL
106 NULL
107 201001001
109 201001002
110 201001003
111 201002001

希望的结果是
107 201001001
109 201001002
110 201001003
111 201002001
108 NULL
106 NULL

请问,语句能实现么?
...全文
123 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2011-02-12
  • 打赏
  • 举报
回复
如果是9位的数字,最大的就是9个9 了,用isnull把null处理成9个9,个人认为即可满足你的排序要求。
叶子 2011-02-12
  • 打赏
  • 举报
回复

declare @table table (id int,bookCode int)
insert into @table
select 106,null union all
select 107,201001001 union all
select 108,null union all
select 109,201001002 union all
select 110,201001003 union all
select 111,201002001

select * from @table order by isnull(bookCode,999999999),id desc
/*
id bookCode
----------- -----------
107 201001001
109 201001002
110 201001003
111 201002001
108 NULL
106 NULL
*/
Linares 2011-02-12
  • 打赏
  • 举报
回复
#2,#5,#6:

select * from tb order by (case when bookcode is null then 1 else 0 end),bookcode,id

否则无法保证bookcode升序
feng2112 2011-02-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiao_ai_mei 的回复:]

SQL code
order by case when bookCode is null then 1 else 0 end,bookCode ,id desc
[/Quote]
这个不错
快溜 2011-02-12
  • 打赏
  • 举报
回复

create table tb (id int,bookcode varchar(10))
insert into tb

select 106,null union all
select 108,null union all
select 107,'201001001' union all
select 109,'201001002' union all
select 110,'201001003' union all
select 111,'201002001'
drop table tb

select * from tb order by (case when bookcode is null then 1 else 0 end),id

/*
id bookcode
----------- ----------
107 201001001
109 201001002
110 201001003
111 201002001
106 NULL
108 NULL
Linares 2011-02-12
  • 打赏
  • 举报
回复
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(id int, bookCode int)
insert into #
select 106, null union all
select 107, 201001001 union all
select 108, null union all
select 109, 201001002 union all
select 110, 201001003 union all
select 111, 201002001

select * from # order by isnull(bookCode,'999999999'), id desc

/*
id bookCode
----------- -----------
107 201001001
109 201001002
110 201001003
111 201002001
108 NULL
106 NULL
*/
打一壶酱油 2011-02-12
  • 打赏
  • 举报
回复


select * from tb order by bookCode ,id desc
Xiao_Ai_Mei 2011-02-12
  • 打赏
  • 举报
回复
order by case when bookCode is null then 1 else 0 end,bookCode ,id desc 
快溜 2011-02-12
  • 打赏
  • 举报
回复

select * from tb order by bookCode desc,id
abuying 2011-02-12
  • 打赏
  • 举报
回复
select id, isnull(bookCode,' ') as bookCode
from @table
order by replace(isnull(bookCode,''),'',999999999),id desc
zhongzuo1981 2011-02-12
  • 打赏
  • 举报
回复
replace 貌似识别不到''
zhongzuo1981 2011-02-12
  • 打赏
  • 举报
回复
不是‘ ’ 是‘’,就是len=0的。这个方法就不行了。

[Quote=引用 12 楼 maco_wang 的回复:]
SQL code

declare @table table (id int,bookCode varchar(12))
insert into @table
select 106,null union all
select 107,'201001001' union all
select 108,null union all
select 109,'201001002' union……
[/Quote]
叶子 2011-02-12
  • 打赏
  • 举报
回复

declare @table table (id int,bookCode varchar(12))
insert into @table
select 106,null union all
select 107,'201001001' union all
select 108,null union all
select 109,'201001002' union all
select 110,'201001003' union all
select 111,'201002001' union all
select 112,' '

select id, isnull(bookCode,' ') as bookCode
from @table
order by isnull(replace(bookCode,' ',999999999),999999999),id desc
/*
id bookCode
----------- ------------
107 201001001
109 201001002
110 201001003
111 201002001
112
108
106
*/
zhongzuo1981 2011-02-12
  • 打赏
  • 举报
回复
一个比较麻烦的情况。
除了有值的行以为,还有一些行不是NULL而是‘’,
所以只能写成
order by (case when bookcode is null then 1 else 0 end),(case when bookcode='' then 1 else 0 end),bookcode,id desc
但再hibernate 里执行 case when 报错。
苦恼!
zhongzuo1981 2011-02-12
  • 打赏
  • 举报
回复
感觉效率上999999999的会高一点吧。
谢谢各位!!!

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧