self join query...

I have the following table (T-SQL included):

personID     person    parentID     threeNumber      
1                     Mike          NULL              1
2                    Jim                1                   1
3                   Peter             2                   1
4                   John              2                   1
5                  Nancy           1                   1
6                  Debi              5                   1
7                  Joan              5                   1
8                 Shila              2                   1

I need to query so it will produce:

personID     parent       childID     threeNumber      
1                     Mike            2                   1
1                     Mike            5                   1
2                    Jim                3                   1
2                    Jim                4                   1
2                    Jim                8                   1
5                  Nancy            1                   6
5                  Nancy            1                   7


Thank you.


if exists (select * from dbo.sysobjects 
  where id = object_id(N'dbo.familyTree') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  drop table dbo.familyTree
go

create table familyTree (
	personID	int identity not null
	, person	varchar(30)
	, parentID	int null
	, threeNumber	int not null
	, constraint pk_personID_familyTree
		primary key (personID)
	, constraint fk_parentID_familyTree
		foreign key (parentID)
		references familyTree (personID)
)
	
go

insert into familyTree (person, parentID, threeNumber) Values
	 ('Mike', null, 1)
	,('Jim', 1, 1)
	,('Peter', 2, 1)
	,('John', 2, 1)
	,('Nancy', 1, 1)
	,('Debi', 5, 1)
	,('Joan', 5, 1)
	,('Shila', 2, 1)
	
	Select * From familyTree

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sachitjainCommented:
select t.PersonId, t.Person, t1.PersonId ChildId, t.threeNumber
from TABLENAME t left join TABLENAME t1 on t1.ParentId = t.PersonId
order by t.PersonId
0
sachitjainCommented:
Use following in case you don't want to see persons with no children

select t.PersonId, t.Person, t1.PersonId ChildId, t.threeNumber
from TABLENAME t inner join TABLENAME t1 on t1.ParentId = t.PersonId
order by t.PersonId
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.