Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Help in displaying parent and child objects

$
0
0
Hi, I would appreciate any help in displaying a list of parent and child objects in a certain way, to give you an idea of what I'm looking to do here's the code to create and populate an example dataset.[code="sql"]CREATE TABLE OrgStructure(orgunitID int,OrgUnitName varchar (100),ParentID int)[/code][code="sql"]INSERT INTO OrgStructureVALUES (1,'OrgUnit 1', 0), (2, 'OrgUnit 2', 0), (3, 'OrgUnit 3', 0), (4, 'OrgUnit 1.1',1), (5, 'OrgUnit 2.1', 2), (6, 'OrgUnit 3.1', 3), (7, 'OrgUnit 1.1.1',4),(8, 'OrgUnit 1.1.2',4),(9, 'OrgUnit 1.1.3',4), (10,'OrgUnit 2.1.1',5),(11, 'OrgUnit 2.1.2',5),(12, 'OrgUnit 2.1.3',5), (13, 'OrgUnit 3.1.1',6),(14, 'OrgUnit 3.1.2',6),(15, 'OrgUnit 3.1.3',6)[/code]The way I'd like to display this data would be:[code="plain"][b]Parent OU Child OU1 Child OU2[/b]OrgUnit 1 OrgUnit 1.1 OrgUnit 1.1.1 NULL NULL OrgUnit 1.1.2NULL NULL OrgUnit 1.1.3OrgUnit 2 OrgUnit 2.1 OrgUnit 2.1.1NULL NULL OrgUnit 2.1.2NULL NULL OrgUnit 2.1.3OrgUnit 3 OrgUnit 3.1 OrgUnit 3.1.1NULL NULL OrgUnit 3.1.2NULL NULL OrgUnit 3.1.3[/code]The 3 OrgUnits with no parentID in one column (parentOU), then any OrgUnits who's parentID matches the orgunitid of the 3 OrgUnits in ParentOU in the next column (Child OU1), followed by any OrgUnits who's parentID matched the orgunitID of the OrgUnits in 'Child OU1' would be in the third column (Child OU2)The dataset I'm working on contains ~700 rows and the OrgUnitNames can be anything, they aren't labelled like the example dataset with 1.1.1 and so on. Let me know if I need to flesh this out a bit more, appreciate any help as I cant seem to get over the line with this one.

Viewing all articles
Browse latest Browse all 3145

Trending Articles