I have a table of System Details which is capable of nesting any numbers of levels deep in Parent Child relationship. Now I need some guidance on how to query the table to get my desired results.My desired results would be displayed as each top level immediately followed by the nested child levels to it.So my desired results would be as follows:ID SID PID Name1 14562 0 ARU12 14562 0 ARU28 14562 2 BRU110 14562 8 CRU19 14562 2 BRU23 14562 0 ARU34 14562 0 ARU4
The current data is stored like this:ID SID PID Name1 14562 0 ARU12 14562 0 ARU23 14562 0 ARU34 14562 0 ARU48 14562 2 BRU19 14562 2 BRU210 14562 8 CRU1
I have included a setup script below for your convenience. Thank you for the help.USE [Test]GO/****** Object: Table [dbo].[SystemDetails] Script Date: 05/29/2013 10:03:29 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[SystemDetails]( [ID] [int] IDENTITY(1,1) NOT NULL, [SID] [int] NOT NULL, [PID] [int] NOT NULL CONSTRAINT [DF_SystemDetails_PID] DEFAULT ((0)), [Name] [varchar](255) NOT NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOSet Identity_Insert [dbo].[SystemDetails] OnINSERT INTO [SystemDetails] ([ID],[SID],[PID],[Name])VALUES(1,14562,0,'ARU1')INSERT INTO [SystemDetails] ([ID],[SID],[PID],[Name])VALUES(2,14562,0,'ARU2')INSERT INTO [SystemDetails] ([ID],[SID],[PID],[Name])VALUES(3,14562,0,'ARU3')INSERT INTO [SystemDetails] ([ID],[SID],[PID],[Name])VALUES(4,14562,0,'ARU4')INSERT INTO [SystemDetails] ([ID],[SID],[PID],[Name])VALUES(8,14562,2,'BRU1')INSERT INTO [SystemDetails] ([ID],[SID],[PID],[Name])VALUES(9,14562,2,'BRU2')INSERT INTO [SystemDetails] ([ID],[SID],[PID],[Name])VALUES(10,14562,8,'CRU1')Set Identity_Insert [dbo].[SystemDetails] OffGOSelect *From SystemDetailsGODrop Table SystemDetailsGO
JBelthoff› As far as myself... I do this for fun!