这是我提出的第一个问题(永远)。所以,在我试图解释我的问题时请耐心等待。
我有一个如下表格,它给出了某个帐户的层次结构:
declare @AccountDefinitions table
(
AccountID int identity(1, 1)
primary key
not null,
AccountNumber nvarchar(11) not null,
AccountType nvarchar(1) not null,
SumSign1 smallint,
SumAccount1 nvarchar(11),
SumSign2 smallint,
SumAccount2 nvarchar(11),
SumSign3 smallint,
SumAccount3 nvarchar(11)
)
如果帐户总结(添加到另一个帐户),则父帐户在SumAccount1中。因此,SumAccount1可以再次在AccountNumber中找到。这是普通的父/子层次结构,可以通过多种解决方案中描述的CTE轻松解决。 然而,还有两个父列,名称为SumAccount2和SumAccount3。这意味着一个帐户最多可以有三个父母。但有时只有两个父母或没有父母。在这种情况下,SumAccount3(和/或其他)将为NULL。
基本上,现在我需要一个嵌套的CTE来处理这些可能导致多个不同路径的多个父母。如果一个帐户有三个父母,而且每个父母都有另外三个父母,那么这个帐户可能已经有九个不同的路径。
以下是一些测试数据:
insert into @AccountDefinitions
(AccountNumber, AccountType, SumSign1, SumAccount1, SumSign2,
SumAccount2, SumSign3, SumAccount3)
values (N'K07_005', N'U', 0, null, 0, null, 0, null),
(N'K07_010', N'U', 0, null, 0, null, 0, null),
(N'K07_010A', N'U', 0, null, 0, null, 0, null),
(N'K07_020', N'R', 0, null, 0, null, 0, null),
(N'K07_020A', N'R', 0, null, 0, null, 0, null),
(N'K07_020AA', N'R', 0, null, 0, null, 0, null),
(N'K07_021', N'U', 0, null, 0, null, 0, null),
(N'K07_022', N'U', 0, null, 0, null, 0, null),
(N'K07_025', N'U', 0, null, 0, null, 0, null),
(N'K07_025A', N'U', 0, null, 0, null, 0, null),
(N'K07_035', N'U', 1, N'K07_060', 0, null, 0, null),
(N'K07_060', N'U', 1, N'K07_930', 0, null, 0, null),
(N'K07_090', N'U', 0, null, 0, null, 0, null),
(N'K07_091', N'U', 0, null, 0, null, 0, null),
(N'K07_091P', N'U', 0, null, 1, N'K07_O091', 0, null),
(N'K07_092', N'U', 0, null, 0, null, 0, null),
(N'K07_095', N'U', 0, null, 0, null, 0, null),
(N'K07_096', N'U', 0, null, 0, null, 0, null),
(N'K07_100', N'U', 0, null, 1, N'K07_1000', 0, null),
(N'K07_1000', N'I', 1, N'K07_O100', 0, null, 0, null),
(N'K07_1010', N'I', 1, N'K07_100', 1, N'K07_101P', 1, N'K07_C130'),
(N'K07_101P', N'U', 1, N'K07_O101', 0, null, 0, null),
(N'K07_110', N'I', 1, N'K07_130', 0, null, 0, null),
(N'K07_120', N'I', 1, N'K07_130', 0, null, 0, null),
(N'K07_120P', N'U', 1, N'K07_O120', 0, null, 0, null),
(N'K07_160', N'I', 1, N'K07_170', 0, null, 0, null),
(N'K07_170', N'I', 1, N'K07_251', 0, null, 0, null),
(N'K07_180', N'U', 1, N'K07_190', 0, null, 0, null),
(N'K07_185', N'S', 1, N'K07_190', 0, null, 0, null),
(N'K07_190', N'S', 0, null, 0, null, 0, null),
(N'K07_200', N'I', 1, N'K07_2000', 1, N'K07_250', 0, null),
(N'K07_2000', N'U', 0, null, 0, null, 0, null),
(N'K07_201', N'I', 1, N'K07_C810', 1, N'K07_900', 1, N'K07_200'),
(N'K07_202', N'I', 1, N'K07_202P', 1, N'K07_251', 0, null),
(N'K07_202P', N'U', 0, null, 0, null, 0, null),
(N'K07_205', N'C', 1, N'K07_091P', -1, N'K07_251', 0, null),
(N'K07_250', N'U', 1, N'K07_300', 1, N'K07_2500', 0, null),
(N'K07_2500', N'U', 0, null, 0, null, 0, null),
(N'K07_251', N'I', 1, N'K07_301', 1, N'K07_251P', 0, null),
(N'K07_251P', N'U', 0, null, 0, null, 0, null),
(N'K07_270', N'T', -1, N'K07_300', 1, N'K07_2700', 0, null),
(N'K07_2700', N'T', 1, N'K07_O270', 0, null, 0, null),
(N'K07_271', N'C', -1, N'K07_301', 1, N'K07_271P', 0, null),
(N'K07_271P', N'T', 0, null, 1, N'K07_O271', 0, null),
(N'K07_280', N'T', -1, N'K07_300', 1, N'K07_2800', 0, null),
(N'K07_2800', N'T', 1, N'K07_O280', 0, null, 0, null),
(N'K07_281', N'C', -1, N'K07_301', 1, N'K07_281P', 0, null),
(N'K07_281P', N'T', 0, null, 1, N'K07_O281', 0, null),
(N'K07_300', N'U', 1, N'K07_400', 1, N'K07_3000', 0, null),
(N'K07_3000', N'U', 0, null, 0, null, 0, null),
(N'K07_301', N'I', 1, N'K07_401', 1, N'K07_301P', 0, null),
(N'K07_301P', N'U', 0, null, 0, null, 0, null),
(N'K07_320', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_321', N'I', 1, N'K07_401', 1, N'K07_342', 0, null),
(N'K07_322', N'I', 1, N'K07_401', 1, N'K07_342', 0, null),
(N'K07_330', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_331', N'I', 1, N'K07_401', 0, null, 0, null),
(N'K07_340', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_341', N'I', 1, N'K07_401', 1, N'K07_342', 0, null),
(N'K07_342', N'C', 0, null, 0, null, 0, null),
(N'K07_350', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_351', N'C', 1, N'K07_401', 0, null, 0, null),
(N'K07_400', N'U', 1, N'K07_600', 0, null, 0, null),
(N'K07_401', N'C', 1, N'K07_601', 1, N'K07_510', 0, null),
(N'K07_420', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_421', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_430', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_431', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_440', N'T', 0, null, 0, null, 0, null),
(N'K07_441', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_450', N'T', 0, null, 0, null, 0, null),
(N'K07_451', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_455', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_456', N'U', 1, N'K07_465', 0, null, 0, null),
(N'K07_457', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_458', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_459', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_460', N'T', 0, null, 0, null, 0, null),
(N'K07_461', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_462', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_463', N'T', 1, N'K07_501', 0, null, 0, null),
(N'K07_464', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_465', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_466', N'C', 0, null, 1, N'K07_502', 0, null),
(N'K07_470', N'T', 0, null, 0, null, 0, null),
(N'K07_471', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_472', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_473', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_474', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_475', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_476', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_480', N'T', 0, null, 0, null, 0, null),
(N'K07_481', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_482', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_483', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_484', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_485', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_488', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_489', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_490', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_491', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_492', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_500', N'T', -1, N'K07_600', 0, null, 0, null),
(N'K07_501', N'T', -1, N'K07_601', 0, null, 0, null),
(N'K07_502', N'C', -1, N'K07_510', 0, null, 0, null),
(N'K07_510', N'C', 0, null, 0, null, 0, null),
(N'K07_520', N'T', -1, N'K07_600', 0, null, 0, null),
(N'K07_521', N'C', -1, N'K07_601', 1, N'K07_466', 0, null),
(N'K07_522', N'C', -1, N'K07_601', 1, N'K07_466', 0, null),
(N'K07_530', N'U', 1, N'K07_600', 0, null, 0, null),
(N'K07_531', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_532', N'C', -1, N'K07_601', 1, N'K07_703', 1, N'K07_502'),
(N'K07_533', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_540', N'U', 1, N'K07_600', 0, null, 0, null),
(N'K07_541', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_551', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_561', N'C', -1, N'K07_601', -1, N'K07_591', 0, null),
(N'K07_571', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_581', N'C', -1, N'K07_601', -1, N'K07_591', 0, null),
(N'K07_591', N'C', 0, null, 0, null, 0, null),
(N'K07_600', N'U', 1, N'K07_700', 0, null, 0, null),
(N'K07_601', N'C', 1, N'K07_701', 0, null, 0, null),
(N'K07_620', N'T', 1, N'K07_700', 0, null, 0, null),
(N'K07_621', N'C', 1, N'K07_701', 0, null, 0, null),
(N'K07_700', N'U', 0, null, 0, null, 0, null),
(N'K07_703', N'T', 1, N'K07_706', 1, N'K07_704', 0, null),
(N'K07_704', N'T', 1, N'K07_707', 0, null, 0, null),
(N'K07_705', N'T', 1, N'K07_706', 0, null, 0, null),
(N'K07_705CO', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705GA', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705MS', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705OT', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705RD', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705SU', N'T', 1, N'K07_707', 0, null, 0, null),
(N'K07_706', N'T', 0, null, 0, null, 0, null),
(N'K07_707', N'T', 0, null, 0, null, 0, null),
(N'K07_710', N'U', 1, N'K07_760', 1, N'K07_7100', 1, N'K07_C050'),
(N'K07_7100', N'U', 1, N'K07_O710', 0, null, 0, null),
(N'K07_720', N'U', 1, N'K07_760', 1, N'K07_7200', 1, N'K07_C055'),
(N'K07_7200', N'U', 1, N'K07_O720', 0, null, 0, null),
(N'K07_7300', N'U', 1, N'K07_O730', 0, null, 0, null),
(N'K07_7400', N'U', 1, N'K07_O740', 0, null, 0, null),
(N'K07_7500', N'U', 1, N'K07_O750', 0, null, 0, null),
(N'K07_760', N'U', 1, N'K07_910', 1, N'K07_7600', 1, N'K07_C075'),
(N'K07_7600', N'U', 0, null, 0, null, 0, null),
(N'K07_800', N'U', 1, N'K07_810', 0, null, 0, null),
(N'K07_800N', N'U', 1, N'K07_810N', 0, null, 0, null),
(N'K07_805', N'U', 1, N'K07_810', 0, null, 0, null),
(N'K07_805N', N'U', 1, N'K07_810N', 0, null, 0, null),
(N'K07_810', N'U', 0, null, 0, null, 0, null),
(N'K07_810N', N'U', 0, null, 0, null, 0, null),
(N'K07_815', N'U', 1, N'K07_825', 0, null, 0, null),
(N'K07_815N', N'U', 1, N'K07_825N', 0, null, 0, null),
(N'K07_820', N'U', 1, N'K07_825', 0, null, 0, null),
(N'K07_820N', N'U', 1, N'K07_825N', 0, null, 0, null),
(N'K07_825', N'U', 0, null, 0, null, 0, null),
(N'K07_825N', N'U', 0, null, 0, null, 0, null),
(N'K07_830', N'U', 1, N'K07_840', 0, null, 0, null),
(N'K07_830N', N'U', 1, N'K07_840N', 0, null, 0, null),
(N'K07_835', N'U', 1, N'K07_840', 0, null, 0, null),
(N'K07_835N', N'U', 1, N'K07_840N', 0, null, 0, null),
(N'K07_840', N'U', 0, null, 0, null, 0, null),
(N'K07_840N', N'U', 0, null, 0, null, 0, null),
(N'K07_845', N'U', 1, N'K07_855', 0, null, 0, null),
(N'K07_845N', N'U', 1, N'K07_855N', 0, null, 0, null),
(N'K07_850', N'U', 1, N'K07_855', 0, null, 0, null),
(N'K07_850N', N'U', 1, N'K07_855N', 0, null, 0, null),
(N'K07_855', N'U', 0, null, 0, null, 0, null),
(N'K07_855N', N'U', 0, null, 0, null, 0, null),
(N'K07_860', N'U', 1, N'K07_870', 0, null, 0, null),
(N'K07_860N', N'U', 1, N'K07_870N', 0, null, 0, null),
(N'K07_865', N'U', 1, N'K07_870', 0, null, 0, null),
(N'K07_865N', N'U', 1, N'K07_870N', 0, null, 0, null),
(N'K07_870', N'U', 0, null, 0, null, 0, null),
(N'K07_870N', N'U', 0, null, 0, null, 0, null),
(N'K07_875', N'U', 1, N'K07_885', 0, null, 0, null),
(N'K07_875N', N'U', 1, N'K07_885N', 0, null, 0, null),
(N'K07_880', N'U', 1, N'K07_885', 0, null, 0, null),
(N'K07_880N', N'U', 1, N'K07_885N', 0, null, 0, null),
(N'K07_885', N'U', 0, null, 0, null, 0, null),
(N'K07_885N', N'U', 0, null, 0, null, 0, null),
(N'K07_900', N'U', -1, N'K07_920', 0, null, 0, null),
(N'K07_905', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_910', N'U', -1, N'K07_940', 0, null, 0, null),
(N'K07_912', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_915', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_918', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_920', N'U', 1, N'K07_940', 0, null, 0, null),
(N'K07_930', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_940', N'U', 0, null, 0, null, 0, null),
(N'K07_C010', N'U', 1, N'K07_C045', 1, N'K07_C130', -1, N'K07_C050'),
(N'K07_C015', N'U', 1, N'K07_C045', 1, N'K07_C135', -1, N'K07_C055'),
(N'K07_C045', N'U', 0, null, 0, null, -1, N'K07_C075'),
(N'K07_C050', N'U', 0, null, 0, null, 0, null),
(N'K07_C055', N'U', 0, null, 0, null, 0, null),
(N'K07_C060', N'U', 0, null, 0, null, 0, null),
(N'K07_C065', N'U', 0, null, 0, null, 0, null),
(N'K07_C070', N'U', 0, null, 0, null, 0, null),
(N'K07_C075', N'U', 0, null, 0, null, 0, null),
(N'K07_C100', N'U', 1, N'K07_C125', 1, N'K07_C130', 0, null),
(N'K07_C105', N'U', 1, N'K07_C125', 1, N'K07_C135', 0, null),
(N'K07_C125', N'U', 0, null, 0, null, 0, null),
(N'K07_C130', N'U', 1, N'K07_C155', 1, N'K07_C630', 0, null),
(N'K07_C135', N'U', 1, N'K07_C155', 1, N'K07_C635', 0, null),
(N'K07_C155', N'U', 0, null, 0, null, 0, null),
(N'K07_C400', N'U', 1, N'K07_C425', 0, null, 0, null),
(N'K07_C405', N'U', 1, N'K07_C425', 0, null, 0, null),
(N'K07_C425', N'U', 0, null, 0, null, 0, null),
(N'K07_C430', N'U', 1, N'K07_C455', 1, N'K07_C580', 1, N'K07_C4300'),
(N'K07_C4300', N'U', 1, N'K07_O430', 0, null, 0, null),
(N'K07_C435', N'U', 1, N'K07_C455', 1, N'K07_C585', 1, N'K07_C4350'),
(N'K07_C4350', N'U', 1, N'K07_O435', 0, null, 0, null),
(N'K07_C4400', N'U', 1, N'K07_O440', 0, null, 0, null),
(N'K07_C4450', N'U', 1, N'K07_O445', 0, null, 0, null),
(N'K07_C4500', N'U', 1, N'K07_O450', 0, null, 0, null),
(N'K07_C455', N'U', 1, N'K07_C4550', 1, N'K07_C890', -1, N'K07_C810'),
(N'K07_C4550', N'U', 0, null, 0, null, 0, null),
(N'K07_C460', N'U', 1, N'K07_C485', -1, N'K07_C490', 1, N'K07_C580'),
(N'K07_C465', N'U', 1, N'K07_C485', -1, N'K07_C495', 1, N'K07_C585'),
(N'K07_C485', N'U', -1, N'K07_C515', 0, null, 0, null),
(N'K07_C490', N'U', 0, null, 0, null, 0, null),
(N'K07_C495', N'U', 0, null, 0, null, 0, null),
(N'K07_C500', N'U', 0, null, 0, null, 0, null),
(N'K07_C505', N'U', 0, null, 0, null, 0, null),
(N'K07_C510', N'U', 0, null, 0, null, 0, null),
(N'K07_C515', N'U', 0, null, 0, null, 0, null),
(N'K07_C520', N'U', 1, N'K07_C545', 1, N'K07_C490', 1, N'K07_C5200'),
(N'K07_C5200', N'U', 1, N'K07_O520', 0, null, 0, null),
(N'K07_C525', N'U', 1, N'K07_C545', 1, N'K07_C495', 1, N'K07_C5250'),
(N'K07_C5250', N'U', 1, N'K07_O525', 0, null, 0, null),
(N'K07_C5300', N'U', 1, N'K07_O530', 0, null, 0, null),
(N'K07_C5350', N'U', 1, N'K07_O535', 0, null, 0, null),
(N'K07_C5400', N'U', 1, N'K07_O540', 0, null, 0, null),
(N'K07_C545', N'U', 0, null, 1, N'K07_C515', 1, N'K07_C5450'),
(N'K07_C5450', N'U', 0, null, 0, null, 0, null),
(N'K07_C550', N'U', 1, N'K07_C575', 1, N'K07_C580', 0, null),
(N'K07_C555', N'U', 1, N'K07_C575', 1, N'K07_C585', 0, null),
(N'K07_C575', N'U', 0, null, 0, null, 0, null),
(N'K07_C580', N'U', 1, N'K07_C599', 1, N'K07_C730', 0, null),
(N'K07_C585', N'U', 1, N'K07_C599', 1, N'K07_C735', 0, null),
(N'K07_C599', N'U', 0, null, 0, null, 0, null),
(N'K07_C600', N'U', 1, N'K07_C625', 1, N'K07_C630', 0, null),
(N'K07_C605', N'U', 1, N'K07_C625', 1, N'K07_C635', 0, null),
(N'K07_C625', N'U', 0, null, 0, null, 0, null),
(N'K07_C630', N'U', 1, N'K07_C655', 0, null, 0, null),
(N'K07_C635', N'U', 1, N'K07_C655', 0, null, 0, null),
(N'K07_C655', N'U', 0, null, 0, null, 0, null),
(N'K07_C700', N'U', 1, N'K07_C725', 1, N'K07_C730', 0, null),
(N'K07_C705', N'U', 1, N'K07_C725', 1, N'K07_C735', 0, null),
(N'K07_C725', N'U', 0, null, 0, null, 0, null),
(N'K07_C730', N'U', 1, N'K07_C755', 0, null, 0, null),
(N'K07_C735', N'U', 1, N'K07_C755', 0, null, 0, null),
(N'K07_C755', N'U', 0, null, 0, null, 0, null),
(N'K07_C800', N'U', -1, N'K07_C890', 0, null, 0, null),
(N'K07_C810', N'U', 0, null, 0, null, 0, null),
(N'K07_C830', N'U', -1, N'K07_C890', 0, null, 0, null),
(N'K07_C870', N'U', -1, N'K07_C890', 0, null, 0, null),
(N'K07_C890', N'U', 0, null, 0, null, 0, null),
(N'K07_I110', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I120', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I130', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I140', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I150', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I160', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I170', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I180', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I190', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I200', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I290', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I295', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I300', N'T', 0, null, 0, null, 0, null),
(N'K07_I310', N'C', 1, N'K07_I400', 0, null, 0, null),
(N'K07_I320', N'C', 1, N'K07_I400', 0, null, 0, null),
(N'K07_I400', N'C', 0, null, 0, null, 0, null),
(N'K07_O091', N'U', -1, N'K07_O251', 0, null, 0, null),
(N'K07_O100', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O101', N'U', 1, N'K07_O202', 0, null, 0, null),
(N'K07_O115', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O116', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O117', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O118', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O120', N'U', 1, N'K07_O202', 0, null, 0, null),
(N'K07_O200', N'U', 1, N'K07_O250', 0, null, 0, null),
(N'K07_O202', N'U', 1, N'K07_O251', 0, null, 0, null),
(N'K07_O250', N'U', 1, N'K07_O300', 0, null, 0, null),
(N'K07_O251', N'U', 1, N'K07_O301', 0, null, 0, null),
(N'K07_O270', N'T', -1, N'K07_O300', 0, null, 0, null),
(N'K07_O271', N'T', -1, N'K07_O301', 0, null, 0, null),
(N'K07_O280', N'T', -1, N'K07_O300', 0, null, 0, null),
(N'K07_O281', N'T', -1, N'K07_O301', 0, null, 0, null),
(N'K07_O300', N'U', 0, null, 0, null, 0, null),
(N'K07_O301', N'U', 0, null, 0, null, 0, null),
(N'K07_O430', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O435', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O440', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O445', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O450', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O455', N'U', 0, null, 0, null, 0, null),
(N'K07_O520', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O525', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O530', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O535', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O540', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O545', N'U', 0, null, 0, null, 0, null),
(N'K07_O710', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O720', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O730', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O740', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O750', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O760', N'U', 0, null, 0, null, 0, null),
(N'K07_P091', N'U', -1, N'K07_P251', 1, N'K07_O091', 0, null),
(N'K07_P091US', N'U', -1, N'K07_P251US', 0, null, 0, null),
(N'K07_P101', N'U', 1, N'K07_P1010', 1, N'K07_PP101', 0, null),
(N'K07_P115', N'U', 1, N'K07_O115', 1, N'K07_P200', 0, null),
(N'K07_P116', N'U', 1, N'K07_O116', 1, N'K07_P200', 0, null),
(N'K07_P117', N'U', 1, N'K07_O117', 1, N'K07_P200', 0, null),
(N'K07_P118', N'U', 1, N'K07_O118', 1, N'K07_P200', 0, null),
(N'K07_P120', N'U', 1, N'K07_O120', 1, N'K07_P202', 0, null),
(N'K07_P120US', N'U', 0, null, 1, N'K07_P202US', 0, null),
(N'K07_P200', N'U', 1, N'K07_P250', 0, null, 0, null),
(N'K07_P202', N'U', 1, N'K07_P251', 0, null, 0, null),
(N'K07_P202US', N'U', 1, N'K07_P251US', 0, null, 0, null),
(N'K07_P250', N'U', 1, N'K07_P300', 0, null, 0, null),
(N'K07_P251', N'U', 1, N'K07_P301', 0, null, 0, null),
(N'K07_P251US', N'U', 1, N'K07_P301US', 0, null, 0, null),
(N'K07_P270', N'T', -1, N'K07_P300', 1, N'K07_O270', 0, null),
(N'K07_P271', N'T', -1, N'K07_P301', 1, N'K07_O271', 0, null),
(N'K07_P271US', N'T', -1, N'K07_P301US', 0, null, 0, null),
(N'K07_P280', N'T', -1, N'K07_P300', 1, N'K07_O280', 0, null),
(N'K07_P281', N'T', -1, N'K07_P301', 1, N'K07_O281', 0, null),
(N'K07_P281US', N'T', -1, N'K07_P301US', 0, null, 0, null),
(N'K07_P300', N'U', 0, null, 0, null, 0, null),
(N'K07_P301', N'U', 0, null, 0, null, 0, null),
(N'K07_P301US', N'U', 0, null, 0, null, 0, null),
(N'K07_P430', N'U', 1, N'K07_O430', 1, N'K07_P455', 0, null),
(N'K07_P435', N'U', 1, N'K07_O435', 1, N'K07_P455', 0, null),
(N'K07_P440', N'U', 1, N'K07_O440', 1, N'K07_P455', 0, null),
(N'K07_P445', N'U', 1, N'K07_O445', 1, N'K07_P455', 0, null),
(N'K07_P450', N'U', 1, N'K07_O450', 1, N'K07_P455', 0, null),
(N'K07_P455', N'U', 0, null, 0, null, 0, null),
(N'K07_P489US', N'T', 0, null, 0, null, 0, null),
(N'K07_P520', N'U', 1, N'K07_O520', 1, N'K07_P545', 0, null),
(N'K07_P521US', N'T', 0, null, 0, null, 0, null),
(N'K07_P525', N'U', 1, N'K07_O525', 1, N'K07_P545', 0, null),
(N'K07_P530', N'U', 1, N'K07_O530', 1, N'K07_P545', 0, null),
(N'K07_P535', N'U', 1, N'K07_O535', 1, N'K07_P545', 0, null),
(N'K07_P540', N'U', 1, N'K07_O540', 1, N'K07_P545', 0, null),
(N'K07_P545', N'U', 0, null, 0, null, 0, null),
(N'K07_P710', N'U', 1, N'K07_O710', 1, N'K07_P760', 0, null),
(N'K07_P710US', N'U', 1, N'K07_P760US', 0, null, 0, null),
(N'K07_P720', N'U', 1, N'K07_O720', 1, N'K07_P760', 0, null),
(N'K07_P720US', N'U', 1, N'K07_P760US', 0, null, 0, null),
(N'K07_P730', N'U', 1, N'K07_O730', 1, N'K07_P760', 0, null),
(N'K07_P740', N'U', 1, N'K07_O740', 1, N'K07_P760', 0, null),
(N'K07_P750', N'U', 1, N'K07_O750', 1, N'K07_P760', 0, null),
(N'K07_P760', N'U', 0, null, 0, null, 0, null),
(N'K07_P760US', N'U', 0, null, 0, null, 0, null),
(N'K07_PA101', N'U', -1, N'K07_PP101', 0, null, 0, null),
(N'K07_PP101', N'U', 0, null, 0, null, 0, null);
我现在的尝试是获得具有多个连接的嵌套层次结构。但这似乎没有做到这一点。
with Hierarchy(AccountNumber, ParentsPaths)
as (
select ad1.AccountNumber,
cast(N'' as nvarchar(max))
from @AccountDefinitions as ad1
where ad1.SumAccount1 is null
union all
select ad2.AccountNumber,
cast(case when h.ParentsPaths = N''
then (cast(ad2.AccountNumber as nvarchar(max)))
else (h.ParentsPaths + '.'
+ cast(ad2.AccountNumber as nvarchar(max)))
end as nvarchar(max))
from @AccountDefinitions as ad2
inner join Hierarchy as h
on h.AccountNumber = ad2.SumAccount1
or h.AccountNumber = ad2.SumAccount2
or h.AccountNumber = ad2.SumAccount3
where ad2.SumAccount1 is not null
)
select *
from Hierarchy
order by len(ParentsPaths) desc
option (maxrecursion 0)
最后,我需要获取所有帐户的列表以及添加它们的所有中间步骤,以便我可以将结果表与所有财务预订一起加入,并将所有值汇总在一起。
示例:对于K07_1000,只有一条可能的路径是K07_1000添加到K07_O100,它加起来为K07_O200,最后以K07_O250结尾。为了能够加入这个,我需要结果:
Original | Mapped
-------------------
K07_1000 | K07_1000 <-- preserve the original posting
K07_1000 | K07_O100 <-- the one and only path
K07_1000 | K07_O200
K07_1000 | K07_O250
K07_1000 | K07_O300
如果我在预订表上加入此表,那么我会在一个预订中记入K07_1000四个结果预订:原始和三个(这将添加到总计的父帐户)。
对于帐户K07_1010,它可能会更难,因为它可能需要四条不同的路径。
Original | Mapped
-------------------
K07_1010 | K07_1010 <-- the original posting
K07_1010 | K07_100 <-- first possible path
K07_1010 | K07_1000
K07_1010 | K07_O100
K07_1010 | K07_O200
K07_1010 | K07_O250
K07_1010 | K07_O300
K07_1010 | K07_101P <-- second possible path
K07_1010 | K07_O101
K07_1010 | K07_O202
K07_1010 | K07_O251
K07_1010 | K07_O301
K07_1010 | K07_C130 <-- third possible path
K07_1010 | K07_C155 <- third path with sub path 1
K07_1010 | K07_C630 <- third path with sub path 2
K07_1010 | K07_C655 <- third path with sub path 2
我希望这是有道理的,有人可以提供帮助。
答案 0 :(得分:1)
拉尔夫,试试吧。我认为它有效:
with Hierarchy
as (
select ad1.AccountNumber,
SumAccount1,
SumAccount2,
SumAccount3
from @AccountDefinitions as ad1
union all
select h.AccountNumber,
ad2.SumAccount1,
ad2.SumAccount2,
ad2.SumAccount3
from @AccountDefinitions as ad2
inner join Hierarchy as h
on h.SumAccount1 = ad2.AccountNumber
or h.SumAccount2 = ad2.AccountNumber
or h.SumAccount3 = ad2.AccountNumber
),
cter
as(
SELECT AccountNumber, AccountNumber AS SumAccount FROM @AccountDefinitions
UNION
select AccountNumber,
SumAccount1 as SumAccount
from Hierarchy
where SumAccount1 is not null
union
select AccountNumber,
SumAccount2 as SumAccount
from Hierarchy
where SumAccount2 is not null
union
select AccountNumber,
SumAccount3 as SumAccount
from Hierarchy
where SumAccount3 is not null)
select * from cter
WHERE AccountNumber = 'K07_1010'
option (maxrecursion 0)