﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / How to Build a single row with groups of fields from several rows per key / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 26 May 2013 00:58:09 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How to Build a single row with groups of fields from several rows per key</title><link>http://www.sqlservercentral.com/Forums/Topic1369453-338-1.aspx</link><description>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.</description><pubDate>Wed, 10 Oct 2012 10:12:48 GMT</pubDate><dc:creator>Avraham de-Haan</dc:creator></item><item><title>RE: How to Build a single row with groups of fields from several rows per key</title><link>http://www.sqlservercentral.com/Forums/Topic1369453-338-1.aspx</link><description>Something like this perhaps?[code="sql"]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[/code]</description><pubDate>Tue, 09 Oct 2012 19:13:15 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: How to Build a single row with groups of fields from several rows per key</title><link>http://www.sqlservercentral.com/Forums/Topic1369453-338-1.aspx</link><description>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</description><pubDate>Mon, 08 Oct 2012 11:11:46 GMT</pubDate><dc:creator>Avraham de-Haan</dc:creator></item><item><title>RE: How to Build a single row with groups of fields from several rows per key</title><link>http://www.sqlservercentral.com/Forums/Topic1369453-338-1.aspx</link><description>[quote][b]Avraham de-Haan (10/6/2012)[/b][hr]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[/quote]Yep.... 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.</description><pubDate>Sun, 07 Oct 2012 07:19:04 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: How to Build a single row with groups of fields from several rows per key</title><link>http://www.sqlservercentral.com/Forums/Topic1369453-338-1.aspx</link><description>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</description><pubDate>Sat, 06 Oct 2012 16:34:21 GMT</pubDate><dc:creator>Avraham de-Haan</dc:creator></item><item><title>RE: How to Build a single row with groups of fields from several rows per key</title><link>http://www.sqlservercentral.com/Forums/Topic1369453-338-1.aspx</link><description>[quote][b]Avraham de-Haan (10/6/2012)[/b][hr]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.[/quote]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.</description><pubDate>Sat, 06 Oct 2012 13:25:06 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>How to Build a single row with groups of fields from several rows per key</title><link>http://www.sqlservercentral.com/Forums/Topic1369453-338-1.aspx</link><description>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.</description><pubDate>Sat, 06 Oct 2012 11:47:54 GMT</pubDate><dc:creator>Avraham de-Haan</dc:creator></item></channel></rss>