x
login about faq Site discussion (meta-askssc)

table transformation into self referencing from CSV data

I have a table that looks like this

                    
Col1,Col2,Col3                    
A,   NULL,NULL                    
A,   B,   NULL                    
A,   B,   C                    

I want to translate this into a self-referencing table: something like...

                    
ID,     ParentID                     
A,      NULL                    
B,      A                    
C,      B                    
...                    

How can I achieve this? I have a few ideas, but is there a fast way? I have a lot of data to convert from the above format. THe problem isn't getting the CSV data in, it is transforming it into something more useable.
Thank you, T. Andreasen

more ▼

asked Nov 26 '09 at 04:12 AM in Default

Ty Andreasen gravatar image

Ty Andreasen
3 1 2 2

can you explain what is COL1, 2, 3 ? how is it transforming to the required table ?

Nov 26 '09 at 04:26 AM Squirrel 1

those columns are varchar(50) and the transformation would convert basically to use ID / ParentID as GUIDs and a 'Text' Field would be the representation of the character.

Nov 26 '09 at 11:03 AM Ty Andreasen

varchar(50) to be exact (where A, B, C, etc. would populate)

Nov 26 '09 at 11:04 AM Ty Andreasen
(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first
            
SELECT *            
INTO #TEMP            
FROM            
(            
SELECT [Col1] = 'A', [Col2] = NULL, [Col3] = NULL            
UNION ALL            
SELECT 'A', 'B', NULL            
UNION ALL            
SELECT 'A', 'B', 'C'            
) AS X            
            
SELECT  [ID] = COALESCE([Col3], [Col2], [Col1]),             
    [ParentID] = CASE WHEN  [Col3] IS NOT NULL THEN [Col2]            
    		WHEN  [Col2] IS NOT NULL THEN [Col1]            
    		ELSE NULL            
    		END            
FROM    #TEMP            
more ▼

answered Nov 26 '09 at 05:05 AM

Kristen gravatar image

Kristen ♦
2.2k 6 7 10

(comments are locked)
10|1200 characters needed characters left
declare @tbl table            
(            
    Col1	char,            
    Col2	char,            
    Col3	char            
)            
insert into @tbl            
select  'A', NULL, NULL	union all            
select  'A', 'B', NULL	union all            
select  'A', 'B', 'C'            
            
select  ID = Col1, ParentID = NULL            
from    @tbl            
where   Col2	is null            
and Col3	is null            
            
union            
            
select  ID = Col1, ParentID = Col2            
from    @tbl            
where   Col2	is not null            
            
union            
            
select  ID = Col2, ParentID = Col3            
from    @tbl            
where   Col3	is not null            
more ▼

answered Nov 26 '09 at 06:39 AM

Squirrel 1 gravatar image

Squirrel 1
1.6k 1 3

(comments are locked)
10|1200 characters needed characters left

I guess my next question would then be: how can I assign unique GUIDS to all of the ID's that I generate, and use parent assignment in this same manner?

more ▼

answered Nov 26 '09 at 12:15 PM

Ty Andreasen gravatar image

Ty Andreasen
3 1 2 2

Please start a new question or update your original question.

Jan 21 '10 at 10:09 PM graz ♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1843
x29

asked: Nov 26 '09 at 04:12 AM

Seen: 705 times

Last Updated: Nov 26 '09 at 05:07 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.