SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Parent-Child Relationship Join
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Brittney10
Posting Yak Master

USA
145 Posts

Posted - 10/31/2013 :  12:42:08  Show Profile  Reply with Quote
I have a table that contains both Parent and Child records and i'm trying to return the Parent data, if a child record has a parent.
For example (ABC is the child and DEF is the Parent):

ID______CNumber_____PNumber___AR Amount
1_______ABC_________DEF_______5,0000
2_______DEF_________NULL______10,000

What I hope to return is this (note Child and Parent records contain the same AR Amount now):

ID______CNumber_____PNumber___AR Amount
1_______ABC_________DEF_______10,000
2_______DEF_________NULL______10,000


James K
Flowing Fount of Yak Knowledge

3326 Posts

Posted - 10/31/2013 :  13:45:02  Show Profile  Reply with Quote
Do you have multiple levels (as in children, grand children, great grand children etc.)? If so, what do you want to display?

If it is just a simple parent child relationship as in your example, this should be sufficient:
SELECT
	c.Id,
	c.CNumber,
	c.PNumber,
	COALESCE(p.ARAmount,c.ARAmount) AS ARAmount
FROM
	YourTable c
	LEFT JOIN YourTable p ON c.PNumber = p.CNumber
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000