读书人

请教这个排序如何做

发布时间: 2012-05-11 12:55:37 作者: rapoo

请问这个排序怎么做?
我有两列数据

SQL code
ID                name---------------------------------1        03013        02024        03065        09046        09057        04028        1101


由于0301比较特殊
现在我想要的结果是 除了0301要按name来排 其他的都按照ID来排

结果为
SQL code
ID                name---------------------------------3        02021        03014        03065        09046        09057        04028        1101

谢谢先

[解决办法]
怎么确定的0301所在行在0202之后。
[解决办法]
SQL code
order by (case when [name] = '0301' then [name] else ltrim(id) end)
[解决办法]
SQL code
create table tb(ID int,name varchar(10))insert into tb select 1,'0301'insert into tb select 3,'0202'insert into tb select 4,'0306'insert into tb select 5,'0904'insert into tb select 6,'0905'insert into tb select 7,'0402'insert into tb select 8,'1101'goselect * from tb order by (case when id=3 then -id else id end)/*ID          name----------- ----------3           02021           03014           03065           09046           09057           04028           1101(7 行受影响)*/godrop table tb
[解决办法]
order by case when name='0301' then 1 else 0 end ,id
[解决办法]
很简单,把name转为数字型,排序就OK了

select id,name from tb order by convert(int,name)
[解决办法]
哦,不好意思,还有0402在,没看清,见笑了
[解决办法]
如果数据多了,是不是0301这行一直就排第二,不会有其它可能?
[解决办法]
SQL code
create table tb(ID int,name varchar(10))insert into tb select 1,'0301'insert into tb select 3,'0202'insert into tb select 4,'0306'insert into tb select 5,'0904'insert into tb select 6,'0905'insert into tb select 7,'0402'insert into tb select 8,'1101'GOSELECT * FROM TB ORDER BY  CASE WHEN NAME='0301' THEN 0 ELSE 1 END ASC,NAME asc
[解决办法]
不是已经搞定了吗,要么就是你需求不明确

读书人网 >SQL Server

热点推荐