SQL Server Collapsing multiple columns into a string from a one to many relationship -


given 1 many item can in many warehouses, doing straight join returns duplicate items. i'd collapse data warehouse table strings there 1 record per item. works, there more efficient approach avoids doing 2 separate subselects/subqueries?

select     im.itemid,     (select warehouseid + ','          stockstatussummary itemkey = im.itemkey xml path (''))      'warehouseids',     (select convert(varchar(20), round(qtyavailable,3)) + ','          stockstatussummary         itemkey = im.itemkey xml path (''))     'warehousequantity' item im 

with output looking this:

itemid  warehouseids  warehousequantity ------- ------------- ----------------- 1       10,20,30,      5,7,9, 2       20,30,40,      6,8,10, 3       30,40,50,      7,9,11, 

there not going method has golden lining, without testing. can try different method see if worth while. start scalar udf method see if worth while. make sure order data, results consistent.

you can find more detail here, http://jahaines.blogspot.in/2009/06/concatenating-column-values-part-1.html.. here performance tests, http://jahaines.blogspot.in/2009/07/concatenating-column-values-part-2.html

edit: wanted add best done in application tier, or clr. bottom line not 1 of sql server's strengths , have try few methods see works best environment. if concerned legibility scalar udf method cleanest, may not scalable.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -