|
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.
(comments are locked)
|
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
(comments are locked)
|
(comments are locked)
|
|
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? Please start a new question or update your original question.
Jan 21 '10 at 10:09 PM
graz ♦
(comments are locked)
|


can you explain what is COL1, 2, 3 ? how is it transforming to the required table ?
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.
varchar(50) to be exact (where A, B, C, etc. would populate)