日期:2014-05-17  浏览次数:20472 次

请大家帮忙提供下,有没有好的思路,谢谢
有这么个需求,有个字段假设叫f_number,想要的最终排序结果如下(规律就是分级排序,三级的在最上面,三级汇总到二级,二级再汇总到一级):
f_number       f_level
CI1.01.01         3
CI1.01.02         3
CI1.01.03         3
CI1.01            2
CI1.02.01         3
CI1.02.02         3
CI1.02.03         3
CI1.02            2
CI1               1
CI2.01.01         3
CI2.01.02         3
CI2.01.03         3
CI2.01            2
CI2.02.01         3
CI2.02.02         3
CI2.02.03         3
CI2.02            2
CI2               1


现在能做到的效果是
SELECT f_number, f_level
FROM  itemTbl
ORDER BY
CASE
WHEN LOCATE('.', item.f_number)> 0 THEN
SUBSTRING(
item.f_number,
1,
LOCATE('.', item.f_number)- 1
)
ELSE
item.f_number
END ASC,
item.f_number DESC


这样查询出的效果是(汇总的效果是正确的,但是按照03,02,01……的顺序降序排序了,应该是按照01,02,03的升序排序):
f_number
CI1.02.03
CI1.02.02
CI1.02.01
CI1.02
CI1.01.03
CI1.01.02
CI1.01.01
CI1.01
CI1
------解决方案--------------------
这是排序的,序列号我再想想
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-24 14:57:11
-- 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: )
--
----------------------------------------------------------------
--> 测试数据:[itemTbl]
if object_id('[itemTbl]') is not null drop table [itemTbl]
go 
create table [itemTbl]([f_number] varchar(9))
insert [itemTbl]
select 'CI1.01.01' union all
select 'CI1.01.02' union all
select 'CI1.01.03' union all
select 'CI1.01' union all
select 'CI1.02.01' union all
select 'CI1.02.02' union all
select 'CI1.02.03' union all
select 'CI1.02' union all
select 'CI1' union all
select 'CI2.01.01' union all
select 'CI2.01.02' union all
select 'CI2.01.03' union all
select 'CI2.01' union all
select 'CI2.02.01' union all
select 'CI2.02.02' union all
select 'CI2.02.03' union all
select 'CI2.02'