sql中,求这样的分类排序的问题,在线等....
- HTML code
分类排序的问题例如表1:num finisted finisttime5 完成 13:101 未处理 2 完成 20:0935 完成 15:124 完成 14:1156 未处理6 完成 23:5810 未处理要达以下面这样的结果:5 完成 13:104 完成 14:1135 完成 15:122 完成 20:096 完成 23:581 未处理 10 未处理 56 未处理我想实现在这样的分类排序: 先按finisted排序, 如果是完成的,再按finisttime排序, 如果是未处理的,再按num排序.MS SQL2000中,这样的SQL语句如何写呢?
[解决办法]
- SQL code
order by finisted,(case when finisted='完成' then finisttime else num)
[解决办法]
- SQL code
if object_id('tb','U') is not null drop table tbgocreate table tb( num int, finisted varchar(10), finisttime varchar(10))goinsert into tbselect 5,'完成','13:10' union allselect 1,'未处理','' union allselect 2,'完成','20:09' union allselect 35,'完成','15:12' union allselect 4,'完成','14:11' union allselect 56,'未处理','' union allselect 6,'完成','23:58' union allselect 10,'未处理',''goselect * from tb order by case when finisted='完成' then 0 else 1 end,finisttime,num/*num finisted finisttime----------- ---------- ----------5 完成 13:104 完成 14:1135 完成 15:122 完成 20:096 完成 23:581 未处理 10 未处理 56 未处理 (8 行受影响)*/
[解决办法]
- SQL code
select * from taborder by finisted,(case when finisted='完成' then finisttime else cast(num as varchar) end)
[解决办法]
- SQL code
use Tempdbgo--> --> if not object_id(N'Tempdb..#T') is null drop table #TGoCreate table #T([num] int,[finisted] nvarchar(3),[finisttime] Datetime)Insert #Tselect 5,N'完成','13:10' union allselect 1,N'未处理',null union allselect 2,N'完成','20:09' union allselect 35,N'完成','15:12' union allselect 4,N'完成','14:11' union allselect 56,N'未处理',null union allselect 6,N'完成','23:58' union allselect 10,N'未处理',nullGoSelect * from #TORDER BY CASE WHEN [finisted]=N'完成' THEN 1 ELSE 2 END,[finisttime],[num]/*num finisted finisttime5 完成 1900-01-01 13:10:00.0004 完成 1900-01-01 14:11:00.00035 完成 1900-01-01 15:12:00.0002 完成 1900-01-01 20:09:00.0006 完成 1900-01-01 23:58:00.0001 未处理 NULL10 未处理 NULL56 未处理 NULL*/