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 2008 Forums
 Transact-SQL (2008)
 Parent and Child Updation
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Kalaiselvan
Posting Yak Master

India
108 Posts

Posted - 11/02/2013 :  22:10:11  Show Profile  Reply with Quote
Hi,
I got a table from SSIS Transformation stored in SQL Server. It has the combination of Name and its Ref Name with its ID's.
Need to split this Table into single row with Parent and Child ID updation.

TABLEA:
[ID] [NAME] [MOBILE] [REF_ID] [REF_NAME] [REF_MOBILE]
101 RAMU 9900040000 185 SURESH 9840078000
101 RAMU 9900040000 177 RAMESH 9850098000
185 SURESH 9840078000 101 RAMU 9900040000
185 SURESH 9840078000 177 RAMESH 9850098000
177 RAMESH 9850098000 185 SURESH 9840078000
177 RAMESH 9850098000 101 RAMU 9900040000

Can retain the 1st ROW as parent or else any other Row, And need to apply the ParentID from REF_ID. Output Table will look like.

[ID] [NAME] [MOBILE] [PARENTID]
101 RAMU 9900040000 0
185 SURESH 9840078000 101
177 RAMESH 9850098000 101


Please help me to fix this with SQL query...


Regards,
Kalai

GhostX1
Starting Member

6 Posts

Posted - 11/04/2013 :  11:04:13  Show Profile  Reply with Quote
I've assumed that the Ref_ID needs to be older (less than the ID).

Select ID, NAME, MOBILE, Case when Min(Ref_ID)<ID Then min(Ref_ID) Else 0 End ParentID
From TableA
Group by ID, NAME, MOBILE

Edited by - GhostX1 on 11/04/2013 11:04:45
Go to Top of Page

Kalaiselvan
Posting Yak Master

India
108 Posts

Posted - 11/05/2013 :  01:00:29  Show Profile  Reply with Quote
It wont work. We cant take MIN REFID for Parent and Child Update.
Its an example with some fields.
For reference I have given some sample Data. Some CIID will refer 4 or 5 ID's and Some ID will refer only one.

Regards,
Kalai
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.03 seconds. Powered By: Snitz Forums 2000