SQL 指定排序 ,, ########### 100% 结贴
-105102311966100127于君35
-155102311955012430蒋远30
-95102301974040890胡正50
05102311971082924李先40
05102311948061725张德30
-1005102311960070425刘昌50
-405102311948111625赖正36
-505102311970120325雷洪50
-585102311973081024艾维40
-35102311955021124蒋道38
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-20 17:09:04
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
--Dec 28 2012 20:23:12
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([a] int,[b] bigint,[name] varchar(4),[c] int)
insert [huang]
select -10,5102311966100127,'于君',35 union all
select -15,5102311955012430,'蒋远',30 union all
select -9,5102301974040890,'胡正',50 union all
select 0,5102311971082924,'李先',40 union all
select 0,5102311948061725,'张德',30 union all
select -100,5102311960070425,'刘昌',50 union all
select -40,5102311948111625,'赖正',36 union all
select -50,5102311970120325,'雷洪',50 union all
select -58,5102311973081024,'艾维',40 union ALL
select -3,5102311955021124,'蒋道',38
--------------开始查询--------------------------
SELECT a,b,name,c
FROM (
select * ,ROW_NUMBER()OVER(ORDER BY GETDATE())id
from [huang])a
ORDER BY CASE WHEN name='赖正' THEN 0 ELSE id END
----------------结果----------------------------
/*
a b name c
----------- -------------------- ---- -----------
-40 5102311948111625 赖正 36
-10 5102311966100127 于君 35
-15 5102311955012430 蒋远 30
-9 5102301974040890 胡正 50
0 5102311971082924 李先 40
0 5102311948061725 张德 30
-100 5102311960070425 刘昌 50
-50 5102311970120325 雷洪 50
-58 5102311973081024 艾维 40
-3 5102311955021124 蒋道 38
*/
if object_id('tb') is not null drop table tb
go
create table tb([a] int,[b] bigint,[name] varchar(4),[c] int)
insert tb
select -10,5102311966100127,'于君',35 union all
select -15,5102311955012430,'蒋远',30 union all
select -9,5102301974040890,'胡正',50 union all
select 0,5102311971082924,'李先',40 union all
select 0,5102311948061725,'张德',30 union all
select -100,5102311960070425,'刘昌',50 union all
select -40,5102311948111625,'赖正',36 union all
select -50,5102311970120325,'雷洪',50 union all
select -58,5102311973081024,'艾维',40 union ALL
select -3,5102311955021124,'蒋道',38
SELECT a,b,name,c
FROM
(
select * ,case when name='赖正'
then 0
else ROW_NUMBER()OVER(ORDER BY @@servername)
end id
from tb
)a
ORDER BY id
/*
abnamec
-405102311948111625赖正36
-105102311966100127于君35
-155102311955012430蒋远30
-95102301974040890胡正50
05102311971082924李先40
05102311948061725张德30
-1005102311960070425刘昌50
-505102311970120325雷洪50
-585102311973081024艾维40
-35102311955021124蒋道38
*/