## How to Build a single row with groups of fields from several rows per key

 Author Message Avraham de-Haan Grasshopper Group: General Forum Members Points: 11 Visits: 18 i have several rows like:Customer sn val1, val2, Val3 ..... (sn is a seq# from 1 to 15)A 1 31 54A 2 98 21 65A 3 44 67 11B ...I want to get A sinle row:Customer val1_1, Val1_2, val1_3, val2_1, val2_2, Val2_3, val3_1, val3_2,.....A 31 54 98 21 65 44 67BThe Single row built will be used to update a database table.Please an efficient solution. Jeff Moden SSC-Forever Group: General Forum Members Points: 44994 Visits: 39880 Avraham de-Haan (10/6/2012)i have several rows like:Customer sn val1, val2, Val3 ..... (sn is a seq# from 1 to 15)A 1 31 54A 2 98 21 65A 3 44 67 11B ...I want to get A sinle row:Customer val1_1, Val1_2, val1_3, val2_1, val2_2, Val2_3, val3_1, val3_2,.....A 31 54 98 21 65 44 67BThe Single row built will be used to update a database table.Please an efficient solution.Of course, "It Depends", but putting it all on one row is likely not the most efficient solution to begin with. We might be able to come up with something very effecient if you explained what these values actually represent. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code:      Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.Although change is inevitable, change for the better is not.Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Avraham de-Haan Grasshopper Group: General Forum Members Points: 11 Visits: 18 It is just an example, my data is more complicated.Actually I have from 0 to 15 data/trailer rows (like orders) for each customer.Now I need to send the customer data _including_ all it's order/trailers in 1 single (long) row.My example imitates these "order" rows which I need to combine into up to 15 "groups" of fields in the output row and glue them to each customer data.I hope I clarified myself.Thanks Jeff Moden SSC-Forever Group: General Forum Members Points: 44994 Visits: 39880 Avraham de-Haan (10/6/2012)It is just an example, my data is more complicated.Actually I have from 0 to 15 data/trailer rows (like orders) for each customer.Now I need to send the customer data _including_ all it's order/trailers in 1 single (long) row.My example imitates these "order" rows which I need to combine into up to 15 "groups" of fields in the output row and glue them to each customer data.I hope I clarified myself.ThanksYep.... knew it was just example data and that's why I asked for a bit more detail.So, each group will have 3 entries (or less as in your example) and each customer can have up to 15 groups. Got that.0. I'm assuming that the data-type in the original table for the 3 entries for each group is INT. Is that correct?1. What do you want to return for a customer with 0 groups and how will that be manifested in the original table.2. Also, since you're sending this to a customer and it sounds a whole lot like they're going to be importing the data either into a spreadsheet or a table, what do you want for delimiters between the "columns" of data in the output? Commas? Tabs? or ...3. ... or do you want this to be a fixed field format? If so, what are the data-types in the 3 values of each group in the original table? Need to know this to be able to handle the max width.Also, since you're kind of new on this forum, please take a look at the article at the first link in my signature line below. Posting readily consumable data (even if it's just example data) like that would make it so we didn't have to ask so many questions to help you. Thanks. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code:      Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.Although change is inevitable, change for the better is not.Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Avraham de-Haan Grasshopper Group: General Forum Members Points: 11 Visits: 18 Thanks for your questions I had to think about it in advance in my explanation.0. I'm assuming that the data-type in the original table for the 3 entries for each group is INT. Is that correct?Mixed in and Nvarchar fields1. What do you want to return for a customer with 0 groups and how will that be manifested in the original table. Nullified fields2. Also, since you're sending this to a customer and it sounds a whole lot like they're going to be importing the data either into a spreadsheet or a table, what do you want for delimiters between the "columns" of data in the output? Commas? Tabs? or ... I am sending it from a WebService so XML data3. ... or do you want this to be a fixed field format? If so, what are the data-types in the 3 values of each group in the original table? Need to know this to be able to handle the max width.It is fixed format so it has 9 fields per customer/group of fields. A customer with no optional trailer rows (Orders?) all its fields will (stay) nullified.The number of Customers handled is of small volume (between 1 to 10?) each with 0 to 15 batches/groups of 9 fields.I hope I am now clear enough, Thanks dwain.c SSCarpal Tunnel Group: General Forum Members Points: 4249 Visits: 6431 Something like this perhaps?`DECLARE @T TABLE (Customer VARCHAR(5), sn INT, val1 INT, val2 INT, Val3 INT)INSERT INTO @TSELECT 'A', 1, 31, 54, NULLUNION ALL SELECT 'A', 2, 98, 21, 65UNION ALL SELECT 'A', 3, 44, 67, 11;WITH MyVals AS ( SELECT Customer, sn, n=1, val=val1 FROM @T UNION ALL SELECT Customer, sn, 2, val2 FROM @T UNION ALL SELECT Customer, sn, 3, val3 FROM @T )SELECT Customer ,val1_1=MAX(CASE WHEN sn=1 AND n=1 THEN val END) ,val1_2=MAX(CASE WHEN sn=1 AND n=2 THEN val END) ,val1_3=MAX(CASE WHEN sn=1 AND n=3 THEN val END) ,val2_1=MAX(CASE WHEN sn=2 AND n=1 THEN val END) ,val2_2=MAX(CASE WHEN sn=2 AND n=2 THEN val END) ,val2_2=MAX(CASE WHEN sn=2 AND n=3 THEN val END) ,val3_1=MAX(CASE WHEN sn=3 AND n=1 THEN val END) ,val3_2=MAX(CASE WHEN sn=3 AND n=2 THEN val END) ,val3_3=MAX(CASE WHEN sn=3 AND n=3 THEN val END) FROM MyValsWHERE val IS NOT NULLGROUP BY Customer` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables Avraham de-Haan Grasshopper Group: General Forum Members Points: 11 Visits: 18 As I have up to 15 "groups" of 9 fields each - this is a long way to go.Only a few customers in each session.