SQL Server - How to concatenate multiple rows into single row, multiple columns

Asked By Marilyn
04-Aug-09 10:53 AM
Here is the task:
1.  A table has components
2.  Each component has layers
Sample:
COKEY        ..fields.. CHKEY | SOL_K1 | SOL_BD1 | SOL_...n
row1    1606010        xxxx   1425      xxxx       xxxx           xxxx
row2    1606010       xxxxx   1426     xxxxx      xxxx           xxxx
row3    1235873       xxxxx    1525    xxxx        xxxx           xxxx
row 4   ....etc.

I need to concatenate all CHKEYs that belong to the same COKEY, and RENUMBER
the columns, starting from CHKEY, by adding the numbers 1 to 10 (maximum
number of layers allowed).  So the final row should have
unique COKEYs.

COKEY  ....CHKEY | SOL_K1 | SOL_BD1 |...| SOLK2 | SOL_BD2 ...
row1  1606010 ----------------------------
row2  1235873

Am I making any sense?  If this can also be done in C#, I'd appreciate any
advise in that direction, too.

Thanks for any help.
CREATE TABLE
(1)
PRIMARY KEY
(1)
DROP TABLE
(1)
RENUMBER
(1)
Keycol
(1)
CHKEY
(1)
  Plamen Ratchev replied...
04-Aug-09 10:58 AM
Here are a few methods for implementing this in T-SQL:
http://www.projectdmx.com/tsql/rowconcatenate.aspx

--
Plamen Ratchev
http://www.SQLStudio.com
  Marilyn replied...
04-Aug-09 11:14 AM
I tried an example but it is good only if you are converting one column with
multiple records; I get error when I include more columns.

Marilyn
  Plamen Ratchev replied...
04-Aug-09 11:36 AM
Try this example:

CREATE TABLE Foo (
keycol INT PRIMARY KEY,
cokey INT,
sol_k1 INT,
sol_bd1 INT);

INSERT INTO Foo VALUES(1, 1606010, 1425, 1);
INSERT INTO Foo VALUES(2, 1606010, 1426, 2);
INSERT INTO Foo VALUES(3, 1235873, 1525, 1);

SELECT DISTINCT
cokey,
STUFF((SELECT '|' + CAST(B.sol_k1 AS VARCHAR(10)) + '|' +
CAST(B.sol_bd1 AS VARCHAR(10))
FROM Foo AS B
WHERE B.cokey = A.cokey
FOR XML PATH('')), 1, 1, '') AS list
FROM Foo AS A;

/*

cokey       list
----------- --------------
1235873     1525|1
1606010     1425|1|1426|2

*/

DROP TABLE Foo;

--
Plamen Ratchev
http://www.SQLStudio.com
  Marilyn replied...
04-Aug-09 06:12 PM
Hi,

Thank you for this.  It worked!

I exported the list field as delimited text file and managed to read them
back into individual columns.

Thanks again.
help
Management Studio Express multiple cascade paths with cascade updates and deletes relationship successfully setup from Table A col1 -> TableB col1 and Table A col2 -> TableB col2. Am I dreaming ? Has someone tried this ? The relationship setup successfully 1) SQL Server 2005 (1) SQL Server (1) MySQL (1) Oracle (1) Stored procedure (1) CREATE TABLE (1) PRIMARY KEY (1) You should not have them if your schema is designed properly. I tell people to take an ER diagram and reduce it to table names and PK-FK paths to make a flow diagram. This lets you see directed 2005 it does not work. Some code for testing: - - 1) self-reference within the same table - - works in IBM DB2 LUW 9.5 - no warning is displayed - - does not work in
Check constraint with a scalar UDF not working SQL Server CREATE TABLE [dbo].[Orders]( [ID] [int] IDENTITY(1, 1) NOT NULL, [Title] [nchar](10) NOT NULL, [Order_Status] [nchar](10) NOT NULL, CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [CK_Orders_Order_Status] CHECK (([Order_Status] = 'Shipped' OR [Order_Status] = 'InProgress')) GO ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [CK_Orders_Order_Status] CREATE TABLE [dbo].[Order_Details]( [ID] [int] IDENTITY(1, 1) NOT
Table and query question SQL Server I would like to create a table that will relate an account to its parent(s). 1 account can have multiple parents parent sof Parent 2. The query should return NULL I was thinking to have a table that has 2 columns: Account and Parent, so in the above example the table will look like this: Parent Account P1 A1 P1 A2 P2 A2 P2 P1 Is that the best table that can reflect it ? And, how can I return the result for question 1 (give Account 1) ? Thank you. SQL Server Programming Discussions SQL Server (1) Oracle (1) Deterministic (1) CREATE TABLE (1) PRIMARY KEY (1) FOREIGN KEY (1) ALTER TABLE (1) INNER JOIN (1) Are Parent
Temp tables vs Permanent table with deletes SQL Server I am looking at changing about 10 procedures that use a Permanent table for nothing more than temporary storage to load and store various records from a select statement and run a Join on this table to update other records. It then deletes all the records it put into the table. The table is used by multiple procedures at the same time keyed by a Guid it always gets at the start of the routines. There are about 4 indexes on this table. There can be any from 1 to 1000 records involved in this. I was thinking about dropping the table and just using a temp table. When the table is created I would then have to create 3 indexes - would drop
Temp table and table variable SQL Server Hi, I have table variable with about 10 columns in my query and I am using it only once. It has about 2000 rows. Query executes about 5 seconds. If I just change table variable into temp table and leave everything the same, query executes 100ms. How is that possible that it is Server Programming Discussions SQL Server 2008 (1) SQL Server (1) SQL Server Books Online (1) CREATE PROCEDURE (1) Stored procedure (1) DROP PROCEDURE (1) CREATE INDEX (1) CREATE TABLE (1) Simon, What is the query you are using? If you