Tuesday, June 10, 2014

Concatenating row data in SQL Server

Saw this T-SQL challenge coming but hated to have to do it because I know how hard it is.

To meet the requirements of a third party system at a client I had to string together multiple values into a single row.  This one is a challenge as illustrated by an excellent article that shows many different ways to accomplish this in SQL.

The "use case" example here is we have one check that pays many invoices.  So we need to output the check information with the related invoice information (number, date) strung together in a semi-colon separated string.

This is not something Transact-SQL is able to manage directly.  Instead you need to pick one of the work-around options from the aforementioned article and go with it.

I first tried the "Table valued UDF with a WHILE loop", but that only returned a single invoice date when I had three of the same invoice dates because it used the MIN() function.

My next choice, that seems to be working so far, is "The blackbox XML method" without the "DISTINCT" clause, again to avoid eliminating duplicate values.  This one uses the FOR XML PATH () clause in SQL

Here is an example:

Check # 123 dated 5/23/2014 pays invoices R21, D44 and X99 dated 3/31/2014, 3/31/2014 and 4/23/2014.  The output should look like this:

Check: 123
Date: May 23, 2014
Invoice Number: R21; D44; X99
Invoice Dates: March 31, 2014; March 31, 2014; April 23, 2014

In essence we are flattening a one-to-many relationship retaining the unique values on the many side.

I'm hoping this technique holds up in testing.

No comments:

Post a Comment