Created
March 21, 2014 07:34
-
-
Save HenrikHoyer/9681378 to your computer and use it in GitHub Desktop.
Build Hierarchial tree of companies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH | |
under_comp(id, alias, level, sort) AS ( | |
select id, alias, 0 as level, cast(alias as varchar(max)) from cdmcomp where id = '16281-----SPPL-SPPL' | |
UNION ALL | |
SELECT cdmcomp.id, cdmcomp.alias, under_comp.level+1, sort + ':' + cdmcomp.alias | |
FROM cdmcomp JOIN under_comp ON cdmcomp.primComp=under_comp.id | |
) | |
SELECT | |
id, | |
SUBSTRING ('..........',1,level*3) + alias | |
FROM under_comp order by sort | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment