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

bom清单分级编号排序问题


字段说明
no_id 想要生成的序号part_id物料编号,parent_id父类编号,qty用量,lev层级,sort_id每层中的各自序号
想通过这样的想法实现,但不知代码如何写
当为第一层时no_id直接=sort_id,以后层级的no_id = 父类的no_id + '.' + sort_id 来实现
1
1.1
1.2
2
2.1
2.1.1
2.2
这样的效果,得到这样的序号后不知道怎样才能这样的排序效果,因为不能直接order by no_id,10会和1放在一起,而把2后到后面的

------解决方案--------------------
SQL code



declare @tb table (id varchar(20))
insert into @tb 
select '1' union all
select '2.1' union all
select '1.1' union all
select '2' union all
select '10' union all
select '2.1.1' union all
select '1.1.1' union all
select '1.1.2' union all
select '10.1' union all
select '3' 
select * 
from @tb 
order by  case when charindex('.',id)=0 then CAST( id AS int) 
          else cast(isnull(LEFT(id,charindex('.',id)-1),id)  as int) end,id

id
1
1.1
1.1.1
1.1.2
2
2.1
2.1.1
3
10
10.1

------解决方案--------------------
上面办法不能解决全部情况,第2位大于10就到了

可以参考http://blog.csdn.net/xys_777/archive/2010/06/15/5672481.aspx