Raj More
Raj More

Reputation: 48048

SSIS transformation (almost like a pivot)

I have the following data coming in to SSIS

Set   Value
---   -------
1     One
1     Two
1     Three
2     Four
2     Five
2     Six

I want to transform it to read

Set   ValueList
---   -------
1     One, Two, Three
2     Four, Five, Six

How do I do this in SSIS?

Upvotes: 2

Views: 1338

Answers (2)

Raj More
Raj More

Reputation: 48048

I used the Script Component to do the string concatenation across rows

string TagId = "-1";
string TagList = "";
bool IsFirstRow = true;

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    if (Row.TAGSId.ToString() == TagId)
    {
        TagList += Row.TAG + ",";
    }
    else
    {
        if (IsFirstRow)
        {
            Output0Buffer.AddRow();
            IsFirstRow = false;
        }

        TagId = Row.TAGSId.ToString();
        TagList = Row.TAG.ToString() + ",";
    }

    Output0Buffer.TagId = int.Parse(TagId);
    Output0Buffer.TagList = TagList;
    Output0Buffer.TagLength = TagList.Length;

    //variable used in subsequent queries
    this.Variables.TagList = TagList;
}

Upvotes: 2

HLGEM
HLGEM

Reputation: 96640

There is a pivot task in the data flow transformations. You could try it, but I'll warn you that we have been less than hapy with it's implementation.

Alternatively, you could use the dataflow to put the data into a staging table, and pivot using SQL or do the pivot in the SQL you use to create the incoming data source. If you want to do it in SQl code, this might help:

select 1 as Item 
into #test 
union select 2 
union select 3 
union select 4 
union select 5 

select STUFF((SELECT  ', ' + cast(Item as nvarchar) 
        FROM #test 
        FOR XML PATH('')), 1, 1, '') 

Upvotes: 1

Related Questions