如何从字母数字范围逐步创建平面父子表?

时间:2016-07-04 18:09:55

标签: sql vb.net algorithm peoplesoft

我从PeopleSoft表中提取了一些层次结构数据,其中包含叶子值的字母和数字范围。我假设这允许快速进入,全部捕获并且对于空间消耗而言是合理的最佳。

我需要打破这一点,以便我以平面格式组合。如果它只是数字,我可以循环遍历数据集,从上限到下限添加一个,但使用字母数字则比这更难。

我认为Peoplesoft正在使用ASCii订购可能的字符,但可能是错误的。

有没有人知道我需要的合理快捷方式?

当前数据格式:

SETID,SETCNTRLVALUE,TREE_NAME,EFFDT,TREE_NODE_NUM,RANGE_FROM,RANGE_TO,TREE_BAANCH,DYNAMIC_RANGE,OLD_TREE_NODE_NUM,LEAF_IMAGE
AAAS10, ,S_DEPTS,2016-04-05 00:00:00.000,1499992131,Z870,Z870, ,N,N, 
AAAS10, ,S_DEPTS,2016-04-05 00:00:00.000,1499992131,Z880,Z880, ,N,N, 
AAAS10, ,S_DEPTS,2016-04-05 00:00:00.000,1499992131,Z881,Z881, ,N,N, 
AAAS10, ,S_DEPTS,2016-04-05 00:00:00.000,1499999761,Z850,Z850, ,N,N, 
AAAS10, ,S_DEPTS,2016-04-05 00:00:00.000,1499999761,Z950,Z950, ,N,N, 
AAAS10, ,S_DEPTS,2016-04-05 00:00:00.000,1499999761,Z951,Z999, ,N,N, 

必需的输出格式:

1499992131,Z870
1499992131,Z880
1499992131,Z881 
1499999761,Z850
1499999761,Z950
1499999761,Z951
1499999761,Z952
1499999761,Z953
...
1499999761,Z998
1499999761,Z999

tree_node_num使用,LEAF

可能值得一提的是,from和to字段可以采用任何形式,并且不能依赖于a1234等

1 个答案:

答案 0 :(得分:0)

我使用UDF来创建动态范围,但是数字(或计数)也会起作用

子查询生成[A-Z]和[000-999]或26,000行的所有组合。您可以再次使用字母表[A-Z]和数字表[000-999]。然后你可以(作为子查询)

Select Letter+Padded_Number from Letters A Join Numbers B on 1=1

示例数据和示例查询

Declare @Table table (Tree_Node_Num varchar(25),Range_From varchar(25),Range_To varchar(25))
Insert Into @Table values 
('1499992131','Z870','Z870'),
('1499992131','Z880','Z880'),
('1499992131','Z881','Z881'),  
('1499999761','Z850','Z850'),
('1499999761','Z951','Z999')

Select Distinct 
       Tree_Node_Num
      ,NewStr
From  @Table A
Join (
        Select NewStr=Alpha+PadNum
         From (Select Cntr=cast(RetVal as int),PadNum=format(RetVal,'000') from [dbo].[udf-Create-Range-Number](0,999,1)) A
         Join (Select RetVal,Alpha=char(RetVal) from [dbo].[udf-Create-Range-Number](65,90,1)) B
           on 1=1
     ) B
 On B.NewStr Between A.Range_From and A.Range_To
 Order by 1,2

返回

Tree_Node_Num   NewStr
1499992131      Z870
1499992131      Z880
1499992131      Z881
1499999761      Z850
1499999761      Z951
1499999761      Z952
1499999761      Z953
...
1499999761      Z997
1499999761      Z998
1499999761      Z999

UDF,以防万一,但是数字/计数表也可以做到这一点

CREATE FUNCTION [dbo].[udf-Create-Range-Number] (@R1 money,@R2 money,@Incr money)

-- Syntax Select * from [dbo].[udf-Create-Range-Number](0,100,2)

Returns 
@ReturnVal Table (RetVal money)

As
Begin
    With NumbTable as (
        Select NumbFrom = @R1
        union all
        Select nf.NumbFrom + @Incr
        From NumbTable nf
        Where nf.NumbFrom < @R2
    )
    Insert into @ReturnVal(RetVal)

    Select NumbFrom from NumbTable Option (maxrecursion 32767)

    Return
End